最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Play Framework submitting boolean values with checkbox? - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Apr 25, 2015 at 0:07 KJ50KJ50 7651 gold badge10 silver badges28 bronze badges 1
  • Did you check in the Network tab of Firefox, Chrome or whichever browser you are using what is actually being sent to the server? Then you can know if the error is in the controller (and the binding) or is directly in the form. – Didac Montero Commented Apr 27, 2015 at 7:29
Add a ment  | 

1 Answer 1

Reset to default 7

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

发布评论

评论列表(0)

  1. 暂无评论