Home > FAQs > Cookbook > Using Checkboxes > Using Checkboxes - Velocity and HTML

Velocity View - edit.vm:

<html>
<body onload="document.forms[0].elements[0].focus()">

<a href="home.vm">Home</a><br/>

#if ($fieldErrors)
  #foreach ($error in $fieldErrors)
    $error<br>
  #end
#end
#if ($actionErrors)
  #foreach ($error in $actionErrors)
    $error<br>
  #end
#end

<form name="edit" action="edit.action" method="post">
<table>
<tr><td>Name</td><td>$user.username</td></tr>
#formRowText("Password" "user.password" $stack.findValue("@cash.validator.PasswordFormatValidator@PASSWORD_MASK"))
#formRowText("Repeat Password" "repeatPassword" $stack.findValue("@cash.validator.PasswordFormatValidator@PASSWORD_MASK"))
#formRowText("Email" "user.email" $!user.email)
#formRowSelect("Language" "user.locale" $stack.findValue("@cash.util.Html@getInstance()").getLocales($locale) $!user.locale.toString())
#formRowSelect("Time Zone" "user.timeZone" $stack.findValue("@cash.util.Html@getInstance()").getTimeZones($locale) $!user.timeZone.ID)
#formRowText("Telephone" "user.telephone" $!user.telephone)
#formRowCheckbox("Locked Out" "user.lockedOut" "true" $user.lockedOut)
#formRowCheckbox("Disabled" "user.disabled" "true" $user.disabled)


#set ($privs = [OS:"boss", "admin", "early", "late", "train"])

#foreach ($priv in $privs)
  #set ($checked = $user.privileges.contains($priv))
  #formRowCheckbox($priv "user.priv" $priv $checked)
#end
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="submit"></td></tr>
</table>

<input type="hidden" name="user.username" value="$user.username">
</form>

</body>
</html>

Velocity Macros - macros.vm:

#macro (formRowText $label $name $value)
  <tr><td><label for="$name">$label</label></td><td><input id="$name" type="text" name="$name" value="$!value"></td></tr>
#end

#macro (formRowSelect $label $name $options $selectedValue)
  <tr><td><label for="$name">$label</label></td><td><select id="$name" name="$name">
#foreach ($option in $options)
<option#if ($option.get(0).equals($selectedValue)) selected#end value="$option.get(0)">$option.get(1)</option>
#end
</select></td></tr>
#end

#macro (formRowCheckbox $label $name $value $checked)
  <tr><td><label for="$name.$value">$label</label></td><td><input id="$name.$value" type="checkbox" name="$name" value="$value"#if ($checked) checked#end ></td></tr>
#end

Note that I don't use the webwork UI tags. (The HTML that comes out of them looks like vomit.)

The HTML generated from above looks like:

<html>
<body onload="document.forms[0].elements[0].focus()">

<a href="home.vm">Home</a><br/>

    
<form name="edit" action="edit.action" method="post">
<table>
<tr><td>Name</td><td>user</td></tr>
  <tr><td><label for="user.password">Password</label></td><td><input id="user.password" type="text" name="user.password" value="********"></td></tr>
  <tr><td><label for="repeatPassword">Repeat Password</label></td><td><input id="repeatPassword" type="text" name="repeatPassword" value="********"></td></tr>

  <tr><td><label for="user.email">Email</label></td><td><input id="user.email" type="text" name="user.email" value="user@example.com"></td></tr>
  <tr><td><label for="user.locale">Language</label></td><td><select id="user.locale" name="user.locale">
<option value="en">English</option>
<option selected value="en_AU">English (Australia)</option>
<option value="en_US">English (United States)</option>
<option value="en_GB">English (United Kingdom)</option>
<option value="es">Spanish</option>
<option value="fr">French</option>

<option value="de">German</option>
</select></td></tr>
  <tr><td><label for="user.timeZone">Time Zone</label></td><td><select id="user.timeZone" name="user.timeZone">
<option selected value="America/Los_Angeles">(GMT-08:00) Los Angeles</option>
<option value="Europe/London">(GMT+00:00) London</option>
<option value="Australia/Brisbane">(GMT+10:00) Brisbane</option>
</select></td></tr>
  <tr><td><label for="user.telephone">Telephone</label></td><td><input id="user.telephone" type="text" name="user.telephone" value="134"></td></tr>
  <tr><td><label for="user.lockedOut.true">Locked Out</label></td><td><input id="user.lockedOut.true" type="checkbox" name="user.lockedOut" value="true" ></td></tr>

  <tr><td><label for="user.disabled.true">Disabled</label></td><td><input id="user.disabled.true" type="checkbox" name="user.disabled" value="true" ></td></tr>



    <tr><td><label for="user.priv.boss">boss</label></td><td><input id="user.priv.boss" type="checkbox" name="user.priv" value="boss" ></td></tr>
    <tr><td><label for="user.priv.admin">admin</label></td><td><input id="user.priv.admin" type="checkbox" name="user.priv" value="admin" ></td></tr>
    <tr><td><label for="user.priv.early">early</label></td><td><input id="user.priv.early" type="checkbox" name="user.priv" value="early" ></td></tr>
    <tr><td><label for="user.priv.late">late</label></td><td><input id="user.priv.late" type="checkbox" name="user.priv" value="late" ></td></tr>

    <tr><td><label for="user.priv.train">train</label></td><td><input id="user.priv.train" type="checkbox" name="user.priv" value="train" ></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="submit"></td></tr>
</table>

<input type="hidden" name="user.username" value="user">
</form>

</body>
</html>