javascript - Play Framework submitting boolean values with checkbox? -
using play 2.3.x trying understand how checkboxes handled in forms. this question seems outdated solution older version of play. understand checkbox info posted if checked, i've created small sample app , no info posted if check boxes. here sample "hello world" application
model
public static class hello { @required public string name; @required @min(1) @max(100) public integer repeat; public string color; public boolean box1; public boolean box2; }
view
@* brevity, here checkbox code *@ <label> <input type="checkbox" name="@helloform("box1").name" value="@helloform("box1").value"> box 1 </label> <label> <input type="checkbox" name="@helloform("box2").name" value="@helloform("box2").value"> box 2 </label>
controller
public static result sayhello() { form<hello> form = form.form(hello.class).bindfromrequest(); if(form.haserrors()) { return badrequest(index.render(form)); } else { hello data = form.get(); logger.debug("box 1 " + data.box1); logger.debug("box 2 " + data.box2); return ok( hello.render(data.name, data.repeat, data.color) ); } }
i want see if can boolean true/false information printed in debug statements. right if click both boxes , submit, return null
. also, know there view helper checkboxes, want understand how working using custom view. any advice on how use checkboxes map boolean attributes?
imagine have following:
<input type="checkbox" name="box1" checked="checked" value="true"> have box1
the field name box1
sent server true
whenever checkbox clicked (or checked). when not checked, nothing sent field.
what set in model (in case, class hello),the boolean field default false:
public boolean box1=false; public boolean box2=false;
in case, when bindfromrequest()
occurs , post method did not send value field box1
and/or box2
, fields filled default (false) value.
on view, thing need, follows (i don't use play helper):
<input type="checkbox" name="@helloform("box1").name" value="true" @if(helloform("box1").value.contains("true")){checked="checked"}> box 1
this check checkbox if field sent view true
Comments
Post a Comment