Search code examples
javaplayframeworkplayframework-2.0playframework-2.3

play framework's input text value always passes 0


I've been working on a play form for a couple of days and am extremely frustrated. I went through the documentation and used the "Play with Java" book, and created a userWeights model class:

public class UserWeights {
        public HashMap<ServicesWeights, Float> userWeights = new HashMap<>();
        @Constraints.Required
        public String user;
        @Constraints.Required
        public String password;
        public int sampleSize;
        public int misSampleSize;
} 

My controller:

public class Application extends Controller {

    private static final Form<UserWeights> userWeightsForm = Form.form(UserWeights.class);

    public static Result index() {
        return ok(index.render("Your new application is ready."));
    }

    public static Result finder () {

        return ok(finder.render(userWeightsForm));
    }

    public static Result runWithUserInput () {
        Form<UserWeights> boundForm = userWeightsForm.bindFromRequest();

        if (boundForm.hasErrors()) { 
            return badRequest(index.render("FAIL"));
        }
        UserWeights weights = boundForm.get(); 

        if (weights.checkboxChoices.get(0) != null && weights.checkboxChoices.get(0).equals("1")) {
            runMethodA();
        } else if (weights.checkboxChoices.get(1) != null && weights.checkboxChoices.get(1).equals("2")) {
            runMethodB();
        } 

        return TODO;
    }

And the view:

@(UserWeightsForm: Form[UserWeights]) 
@import helper._ 
@import helper.twitterBootstrap._ 

@main("finder") {
<h1>Please fill the required fields</h1>
@helper.form(action = routes.Application.runWithUserInput(), 'enctype -> "multipart/form-data") {
<fieldset>
    <legend>Finder</legend>
    @helper.inputText(UserWeightsForm("user"),'_label -> "User")
    @helper.inputPassword(UserWeightsForm("password"), '_label -> "Password")
    <br/>
        <label for="checkboxInput">Input Type:</label><br/>

        <span class="buttonSet" id="checkboxInput"> 
        <input type = "checkbox" id = "genericCheckbox" name = 'checkboxChoices[0]' value = "1">
        <label for = "genericCheckbox">Generic Sample</label>
        <input type  =  "number" name  =  'UserWeightsForm("sampleSize")' id  =  "genericInput" value = "@UserWeightsForm("sampleSize").value"><br/>
        <input type = "checkbox" id = "misCheckbox" name = 'checkboxChoices[1]' value = "2">
        <label for = "misCheckbox">MisSample</label>
        <input type  =  "number" name  =  'UserWeightsForm("misSampleSize")' id  =  "misInput" value = "@UserWeightsForm("misSampleSize").value"><br/>
        </span>

What I want - if the first checkbox is selected, the user will fill in the sampleSize text input field, and that value will be bounded to the form, while the misSampleSize field will be blank/zero/whatever (in this case I'm not using it anyway). And vice versa.

The problem - when I check the checkbox and fill in the sample text input, it binds the value 0 to the form.

I've tried setting the value in the input tag to null, removing it completely and using the template helper (I prefer to avoid it because it's not as flexible). I can't understand why the number I enter into the text field is ignored, and my programs considers it to be 0.

So question 1: How do I stop the 0 value being populated?

question 2 How can I send a value for one of the text fields (sampleSize or misSampleSize), and leave the other one empty, without getting form errors?

Thanks!


Solution

  • I work in Scala, not Java. But you I think you need your input id and name fields to match your form field names. It uses those to bind. You don't show your UserWeightsForm, but you could try something like:

        <input type="number" name='sampleSize' id="sampleSize" value="@UserWeightsForm("sampleSize").value.getOrElse("0")"><br/>
        <input type="number" name='misSampleSize' id="misSampleSize" value="@UserWeightsForm("misSampleSize").value.getOrElse("0")"><br/>
    

    I also use a "getOrElse" on the value in case (error perhaps) there is no value.