12

An app I'm working on using the Play! Framework has an object called gift with a boolean property, called Taken. How do I show the state of this value as a checkbox on my view? I've tried :-

<input id="gift_Taken" class="" type="checkbox" name="gift.Taken" value="true"  />
<input type="hidden" name="gift.Taken" value="false" />

based on examples I've seen from the autogenerated CRUD forms, but the checkbox is not checked when the property is True, which is what I'm aiming for.

Anyone know the correct way to achieve this?

1
  • Just set value='true' on checkbox and in your method signature add a parameter boolean taken it'll do the binding. Commented Jan 22, 2012 at 9:45

3 Answers 3

12

The accepted answer is actually not 100% correct as it doesn't handle the "unchecked" case. To handle both cases you need a hidden field:

<input id="gift_Taken" type="checkbox" name="gift.Taken" ${gift.Taken ? 'checked':''}  />
<input type="hidden" name="gift.Taken" value="false" />

Note that the placement seems to be important, so the hidden field must be after the checkbox.

Writing a custom template tag for this, makes it easy not to forget the hidden input (put this into views/tags/checkbox.html):

<input id="${_id}" type="checkbox" name="${_name}" value="true" #{if _checked} checked="checked" #{/if}>
<input type="hidden" name="${_name}" value="false">

Then call this template like this:

#{checkbox id:'gift_Taken', name: 'gift.Taken', checked: gift.Taken /}

See also the related discussion on the play framework list: https://groups.google.com/forum/?fromgroups=#!topic/play-framework/HygQuYF3a8E

Sign up to request clarification or add additional context in comments.

2 Comments

Though not ideal, another solution is if a default value of false is used in the model, the absence of a submitted checkbox value will implicitly be interpreted as false. Red flags all over, but worth mentioning.
Thanks for the ordering hint (in e.g. php one uses the same solution, but the hidden field is placed before the checkbox)!
9

You just need to set the checked value against the checkbox, if the value is true.

for example (assuming the object sent in from the view is called gift, and the boolean value is called Taken.

<input id="gift_Taken" type="checkbox" name="gift.Taken" ${gift.Taken ? 'checked':''}  />

4 Comments

Perfect - I love ternary operators ;-) Side note, gonna go get that book of yours
The ternary operator is even more powerful in Groovy. Checkout the elvis operator groovy.codehaus.org/…
I tried this and it didn't work. I tried <input type="checkbox" ${true ? 'checked' : ''} /> so this should have it checked all the time. But it doesn't. Any idea why?
@DaoLam, probably too late here, but Play 2.0 requires using something like @if(gift.Taken) { "checked" }. I suspect that this answer is old and applies to a version that did not use @ to have code in templates.
0

If you are able to use javascript, you can use:

$(document).ready(function() {
    $( ":checkbox" ).each(function() {
        var name = $(this).attr("name");
        if(typeof(name) != "undefined") {
            var checkboxString = "";
            checkboxString = '<input type="hidden" style="display:none;" name="'+ name +'" value="false" />'
            $(this).after(checkboxString);
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.