Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework 2.0 form helper: from checkbox to List<String>

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{ ... }?

like image 935
Martin Preusse Avatar asked Jul 19 '12 13:07

Martin Preusse


1 Answers

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
}

UPDATE

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.

like image 63
Wayan Wiprayoga Avatar answered Oct 24 '22 19:10

Wayan Wiprayoga