Play Framework - checkbox handling
How to handle a checkbox in Play! A framework? Should I catch the value as a String on the controller? How do I get the values checked by the user?
This is the html code of my checkbox
#{list items:categories, as:'category'}
<tr>
<td><input type="checkbox" name="category" id="category-${category.name}" value="${category.id}" /><label for="category-${category.name}"> ${category.name}</label></td>
</tr>
#{/list}
source to share
To show the state of a certain term (there are more terms actually, think of a project with multiple terms, that's why you see the name selectedConditions) in the view I made:
#{form @ProjectController.update(project.alias), enctype:'multipart/form-data', class:'well form-horizontal'} #{list terms, as:'term'} <input type="checkbox" name="selectedTerms" value="${term.name}"/> <span>${term.name}<span> #{/list} #{/form}
Now the main question is: how do I know which of these terms were selected by the user?
Well Play let me define these terms as List, check below:
public static void update(String alias, List<String> selectedTerms) { //play with selected terms }
Please note: the selected list of terms will contain ONLY terms that are selected (TRUE) by the user. Because I have their names (or IDs or whatever you want) all problems are solved :)
Ps: You are probably asking: how do you display them in the view after you have saved them in db?
#{list terms, as:'term'} <input type="checkbox" name="selectedTerms" value="${term.name}" ${term.selected ? 'checked':''}/> <span>${term.name}</span> #{/list}
Thanks to Christian Boariu, here is a link for your inspiration:
http://crisdev.wordpress.com/2012/05/19/play-framework-get-checkbox-value-from-view-in-controller/
source to share