玩框架提交布尔值与复选框? [英] Play Framework submitting boolean values with checkbox?

查看:153
本文介绍了玩框架提交布尔值与复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Play 2.3.x我想了解如何在表单中处理复选框。 这个问题好像是旧版Play的过时解决方案。我知道复选框信息只会在选中时发布,但我创建了一个小样本应用程序,即使选中了框,也不会发布任何信息。这里是我的示例Hello World应用程序



模型

  public static class Hello {
@Required public String name;
@Required @Min(1)@Max(100)public Integer repeat;
公共字符串颜色;
public Boolean box1;
public Boolean box2;
}

查看
$ b

  @ *为简单起见,以下是复选框代码* @ 
< label>
< input type =checkbox
name =@ helloForm(box1)。name
value =@ helloForm(box1)。value>方框1
< / label>
< label>
< input type =checkbox
name =@ helloForm(box2)。name
value =@ helloForm(box2)。value>方框2
< / label>

控制器

  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 was+ data.box1);
Logger.debug(Box 2 was+ data.box2);
return ok(
hello.render(data.name,data.repeat,data.color)
);


$ / code>

我想知道是否可以得到布尔值true /错误信息打印在调试语句中。现在,即使我点击两个框并提交,它们也会返回为 null 。此外,我知道有复选框的视图助手,但我想了解如何使用自定义视图来实现此功能。 关于如何使用复选框来映射布尔属性的任何建议?
$ b PS - 完整的代码在这里,如果你想尝试它

解决方案

试想你有以下几点:

 < input type =checkboxname =box1checked =checkedvalue =true>我有一个box1 

名称 box1 将在复选框被点击(或选中)时以 true 的形式发送到服务器。如果没有选中,则不会发送该字段。



我所做的是在模型中设置(在您的情况下,类Hello),布尔字段默认为false:

  public Boolean box1 = false; 
public Boolean box2 = false;

在这种情况下,当 bindFromRequest()发生,并且POST方法没有为字段 box1 和/或 box2 发送任何值,字段将为填充默认值(false)。



在视图中,你需要的唯一东西如下(我不使用游戏助手):

 < input type =复选框
name =@ helloForm(box1)。name
value =true
@if(helloForm(box1).value.contains(true)){checked =checked}>方框1

如果字段以 true


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 checkbox info will only be posted if checked, but I've created a small sample app and no info is posted even if I check the boxes. Here is my 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

    @* For brevity, here is the 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 was " + data.box1);
        Logger.debug("Box 2 was " + data.box2);
        return ok(
                hello.render(data.name, data.repeat, data.color)
        );
    }
}

I want to see if I can get the boolean true/false information printed in the debug statements. Right now even if I click both boxes and submit, they return as null. Also, I know that there is a view helper for checkboxes, but I want to understand how to get this working using a custom view. Any advice on how to use checkboxes to map Boolean attributes?

PS - full code is here if you'd like to try it

解决方案

Imagine you have the following:

<input type="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:

public Boolean box1=false;
public Boolean 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):

<input type="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

这篇关于玩框架提交布尔值与复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆