简体   繁体   English

Play Framework 2.0表单助手:从复选框到列表<String>

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

I have a model that contains a String and a list: 我有一个包含String和列表的模型:

public String title;    
public List<String> topics;

In index.scala.html I use a form to add new items: 在index.scala.html中,我使用表单添加新项:

@form(routes.Application.newPaper()) {
    @inputText(paperForm("title"))
    <input type="submit" value="Create">
    }

with a simple String, this works nicely. 使用简单的String,这很好用。 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; 然后将所有选中的'主题'添加到List<String> topics; of my new item. 我的新项目。 How can I process the checkboxes within @form{ ... }? 如何处理@form {...}中的复选框?

I am using Play!Framework 2.1.0 , below is the solution : 我正在使用Play!Framework 2.1.0 ,下面是解决方案:

1. In the scala template, you must give all checkbox name like this: 1.在scala模板中,您必须提供所有复选框名称,如下所示:

@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 : 2.然后在您的控制器中,作为处理表单提交的操作,您应该执行以下操作

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 UPDATE

This is another idea to the solution. 这是该解决方案的另一个想法 Your checkbox code on scala template is not modified . 您的scala模板上的复选框代码未被修改

@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. 注意:我也在Play!Framework 2.1.1上测试过,这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM