Skip to content Skip to sidebar Skip to footer

Play Framework Submitting Boolean Values With Checkbox?

Using Play 2.3.x I am trying to understand how checkboxes are handled in forms. This question seems like an outdated solution for an older version of Play. I understand that checkb

Solution 1:

Imagine you have the following:

<inputtype="checkbox" name="box1" checked="checked" value="true"> I have a box1

The field with name box1 will be sent to the server as true whenever the checkbox is clicked (or checked). When is not checked, nothing will be sent for this field.

What I do is to set in the model (in your case, the class Hello),the Boolean field by default to false:

publicBoolean box1=false;
publicBoolean box2=false;

In this case, when the bindFromRequest() occurs and the POST method did not send any value for the field box1 and/or box2, the fields will be filled with the default (false) value.

On the view, the only thing you will need, is as follows (I don't use the play helper):

<inputtype="checkbox"
     name="@helloForm("box1").name"
     value="true"     @if(helloForm("box1").value.contains("true")){checked="checked"}> Box 1

This will check your checkbox if the field was sent to the view as true

Post a Comment for "Play Framework Submitting Boolean Values With Checkbox?"