I have a model that contains a String and a list:
public String title;
public List<String> topics;
In index.scala.html I use a form to add new items:
@form(routes.Application.newPaper()) {
@inputText(paperForm("title"))
<input type="submit" value="Create">
}
with a simple String, this works nicely. But I would like to show checkboxes
@for(t <- topics) {
<input type='checkbox' name='topic' value=@t>@t <br>
}
and subsequently add all checked 'topics' to the List<String> topics;
of my new item. How can I process the checkboxes within @form{ ... }?
I am using Play!Framework 2.1.0, below is the solution :
1. In the scala template, you must give all checkbox name like this:
@form(action = routes.Application.newPaper()) {
@inputText(paperForm("title"))
@******* Indexed chekbox name *********@
@for((t, index) <- topics.zipWithIndex) {
<input type="checkbox" name="topics[@index]" value="@t">@t <br>
}
<input type="submit" value="Create">
}
2. Then in your controller, as an action to handle form submit, you should doing something like this :
public static Result newPaper() {
// Bind submitted form value to your model, ex. Paper.java
Form<Paper> paperForm = Form.form(Paper.class).bindFromRequest();
Paper paper = paperForm.get();
Logger.info("Title entered = " + paper.title);
// Because in template we use indexed name, unchecked item are binded with null value
paper.topics.removeAll(Collections.singleton(null)); // remove value for unchecked topic
for (String t : paper.topics) {
Logger.info("The topic is " + t);
}
Logger.info("Total topic selected = " + paper.topics.size());
return redirect(routes.Application.index()); // redirect page
}
This is another idea to the solution. Your checkbox code on scala template is not modified.
@for(t <- topics) {
<input type='checkbox' name='topic' value=@t>@t <br>
}
So the controller should be like this :
public static Result newPaper() {
// Bind submitted form value to your model, ex. Paper.java
Form<Paper> paperForm = Form.form(Paper.class).bindFromRequest();
Paper paper = paperForm.get();
// get request value from submitted form
Map<String, String[]> map = request().body().asFormUrlEncoded();
String[] checkedVal = map.get("topic"); // get selected topics
// assign checked value to model
paper.topics = Arrays.asList(checkedVal);
// for debugging purpose
for (String t : paper.topics) {
Logger.info("The topic is " + t);
}
Logger.info("Total topic selected = " + paper.topics.size());
return redirect(routes.Application.index()); // redirect page
}
Hope this idea is more elegant.. :)
Note: I have tested on Play!Framework 2.1.1 too, and that is work for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With