#{extends '../../main.html'/}# #{set title:'Html Reference'/}# #{set tab:'management'/}# #{renderTagArgs '../docHome.html'/}#

Checkbox

Checkboxes are a little strange in html. Before we show you the webpieces example, let's go over html and what the browser will send to the webserver. Here is the code for a standard text input, a checked checkbox, and an unchecked checkbox in html:

*[  First Name: 

  Is Webpieces Cool: 

  Need More: ]*

If the user posts the above, the browser will send only firstName=&isWebpiecesCool=someValue1 to the webserver. Your POST controller method may take a parameter of

  1. boolean isWebpiecesCool OR
  2. Boolean isWebpiecesCool OR
  3. String isWebpiecesCool

If it is a boolean, then the isWebpiecesCool parameter will only be set to true if the value from your input checkbox element has value="true". If the isWebpiecesCool parameter is a String, webpieces will pass through the string value of the checkbox setting the parameter to the String value

Of course, for whether a checkbox is checked or not, we store it in flash and a special field on the *[#{field}#]* tag *[#{field.checked}#]* will lookup the value in flash and if it doesn't exist will fallback to the original value of the entity

#{form action:@[POST_CHECKBOX]@, class:'form-horizontal'}# #{field 'fun', label: 'Is Fun:'}# #{/field}# #{/form}#

The webpieces html code above is:

*[#{form action:@[POST_CHECKBOX]@, class:'form-horizontal'}#

  #{field 'fun', label: 'Is Fun:'}#
    
  #{/field}#
  
  

#{/form}#]*

The webpieces routes for the above is:

*[scopedBldr.addRoute(BOTH, HttpMethod.GET , "/examples/checkbox", "ExamplesController.checkbox", ExampleRouteId.CHECKBOX);
scopedBldr.addRoute(BOTH, HttpMethod.POST, "/examples/postCheckbox", "ExamplesController.postCheckbox", ExampleRouteId.POST_CHECKBOX);
scopedBldr.addRoute(BOTH, HttpMethod.GET , "/examples/checkboxResult", "ExamplesController.checkboxResult", ExampleRouteId.CHECKBOX_RESULT);]*

The Controller GET and POST methods for this page is:

*[	public Render checkbox() {
		return Actions.renderThis(
				"menu", menuCreator.getMenu(),
				"fun", true,
				"another", "checked");
	}
	
	public Redirect postCheckbox(String fun) {
		//We could put the firstName in the url such as /examples/inputResult/{firstName} 
		//or we could save to database
		//or we can put it in flash and for this example, we put it in flash
		Current.flash().put("isFun", fun);
		Current.flash().keep();
		return Actions.redirect(ExampleRouteId.CHECKBOX_RESULT);
	}]*