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}

      

+1


source to share


2 answers


I think that if you have List<String> category

a form in an action, you will get a list of the values ​​of the checked items.



0


source


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/

0


source







All Articles