POJO Object binding and checkboxes

1,285 views
Skip to first unread message

damian

unread,
Sep 14, 2010, 9:57:04 PM9/14/10
to play-framework
When Play binds request parameters to a POJO it ensures that
"Unresolved parameters are safely ignored." The problem with this is
that if a checkbox is unselected, it will not be in the parameter list
and thus a "false" is never bound to the POJO.

I've worked around this by including checkbox parameters in the method
and setting them in the code, rather than relying on them in the POJO
bind. How have other developers worked around this? It doesn't feel
like something that Play can resolve without accidentally changing a
value when it was not required.

Cheers,

Damian.

GrailsDeveloper

unread,
Sep 15, 2010, 3:54:51 AM9/15/10
to play-framework
That a general problem indeed. I'm not sure, but I think grails solve
this issue by have another parameter with the info that the checkbox
is asked. Here an example:

<tr class="prop">
<td valign="top" class="name">
<label for="aktiv">Aktiv:</label>

</td>
<td valign="top" class="value ">
<input type="hidden" name="_aktiv" />
<input type="checkbox" name="aktiv" checked="checked"
id="aktiv" />
</td>
</tr>

Then you know if the parameter "aktiv" isn't set, but you get the
parameter "_aktiv", you must set the property to false. Would be
indeed great if this would be supported by CRUD (I don't test it) and
by play.

Niels

Guillaume Bort

unread,
Sep 15, 2010, 2:13:56 PM9/15/10
to play-fr...@googlegroups.com
It's already done in CRUD, and you can do the same for your own
checkbox. The idea is to declare an hidden field with the same name:

<input type="checkbox" name="object.done" value="true" />
<input type="hidden" name="object.done" value="false" />

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

damian

unread,
Sep 15, 2010, 6:53:47 PM9/15/10
to play-framework
That's a good tip thanks.

Note in some quick testing that I've just done, the placement of the
hidden field is important. It must be after the checkbox or the value
will always be that of the hidden field.

Cheers,

Damian.
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

GrailsDeveloper

unread,
Sep 16, 2010, 2:35:06 AM9/16/10
to play-framework
I head a look into the HTML4.01 Spec http://www.w3.org/TR/html401/interact/forms.html.
I didn't found any rule that defines how the controls are submitted.
If I follow http://stackoverflow.com/questions/452066/html-form-with-multiple-hidden-control-elements-of-the-same-name.
It seems that the control name "object.done" will be submitted twice
so described as an get url &object.done=true&object.done=false. So
it's part of the server-implementation and play does the decision to
take only the first in the Binder-Class. Well this explain the nice
behavior of play, but one question remains: Is there any spec which
assure that the values comes in the same order than they defined?

Niels

Guillaume Bort

unread,
Sep 16, 2010, 4:20:03 AM9/16/10
to play-fr...@googlegroups.com
Yes that's right.

>>> fram...@googlegroups.com.

GrailsDeveloper

unread,
Sep 16, 2010, 5:18:42 AM9/16/10
to play-framework
Is it right that there exist a spec (which) or is it right that this
is an open question?
Niels

On 16 Sep., 10:20, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Yes that's right.
>

Guillaume Bort

unread,
Sep 16, 2010, 6:06:56 AM9/16/10
to play-fr...@googlegroups.com
I don't know if there is a spec for that. I just know that it works.

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,

write guillau...@gmail.com

Reply all
Reply to author
Forward
0 new messages