Playframework – POJO, JPA binding and check box
Play uses "unresolved parameters are safely ignored. Type mismatches are also safely ignored." When binding parameters to POJOs It works well most of the time
However, this can cause problems with the check box because the unselected check box is not sent in the parameter list
Crud module solves this problem by inserting a hidden input field after the check box This is valid because play only reads the first of the two same parameters to bind
The source from the crud module displays a check box:
<input id="object_isInvoiceable" type="check@R_405_2419@" name="object.isInvoiceable" /> <input type="hidden" name="object.isInvoiceable" value="false" />
Another method is to pass the check box not as a member of the object, but as a separate parameter You can then capture it in the controller and set the object there Member, as follows:
<input id="object_isInvoiceable" type="check@R_405_2419@" name="isInvoiceableExtraParamFromHell" />
And in the controller:
public static void save(Event object,Boolean isInvoiceableExtraParamFromHell) { if(isInvoiceableExtraParamFromHell == null) { object.isInvoiceable = false; } else { object.isInvoiceable = true; } ... validation etc ... object.save(); ... render etc ... }
I don't like alternative 1 because I how to ensure that the order of parameters is sent correctly in all browsers
I don't like alternative 2 because if I change objects, I have to manage individual parameters and modify my controller methods
Is there a better way to do this? I'm new to the play framework, so I may have missed some comments or other things
Edit:
I wasn't sure before The problem is not that when creating a new object, I can set it to default to false, as recommended by codemwnci The problem is when binding to an existing JPA object
From playframework tutorial:
In this case, it does not set the member value to any default value
For example, I already have a JPA object stored in the database with isinvoiceable set to true I render this object to a template If I uncheck the check box now, the parameters will not be sent, so isinvoiceable is still true
Solution
For new objects, you can do the following
If you bind POJOs, it's enough to set default values in POJOs
for example
public class Event extends Model { public Boolean isInvoiceable = Boolean.FALSE; ... ... }
For existing objects, you can slightly modify the theme used by crud by performing a jQuery onclick operation on it so that the value of the check box always sets the hidden value and maps the hidden value to POJO
So, for example
<input id="object_isInvoiceable_check@R_405_2419@" type="check@R_405_2419@" name="ignoredformfield" /> <input id="object_isInvoiceable_formfield" type="hidden" name="object.isInvoiceable" value="false" /> <script>$("#object_isInvoiceable_check@R_405_2419@").click(function( $("object_isInvoiceable_formfield").value("$("#object_isInvoiceable_check@R_405_2419@").is(":checked")"); ));</script>
Note: I haven't checked the JavaScript code, but you see