Accessing a list of params in controller

checkboxgrailsgroovyparametersrequest

Im very new to grails (1.3.7) so please be patient 🙂

I have a gsp where I have various checkboxes. A user can click on them and then send his answer to a controller. The controller is receiving this request correctly.

My problem is, that, for working with what the user chose, I have to check every parameter – to see if this checkbox was really checked. Thats really cumbersome and doesnt work very well, because the page displaying the checkboxes is dynamic – so the checkboxes which can be clicked are dynamic too. In my controller I dont know for which params I have to check then.

Is there any possibility to receive a list of all checkboxes (or better: all checked checkboxes) in my controller? I researched but didnt find an answer!

Thanks for answering! 🙂

[EDIT]

Thank you,

params.name.each{i->
            System.out.println(i);
}

is very simple and works 🙂 It just gives back the checked ones

Best Answer

It must be passed as an extra request parameter (it's a limitation of http). You can add following field into your form, for example:

<input type="hidden" name="checkboxes" value="${myCheckboxesNames.join(',')}"/>

or making same using JavaScript, as it names are dynamic on client side.

BTW, you can also check all request parameters, by

params.each { name, value ->
// so something
}

so if you are using some special prefix/suffix for this checkbox names, it would be:

params.entrySet().findAll {
   it.key.startsWith(prefix)
}.each {
   println "Checkbox $it.key = $it.value"
}
Related Topic