Check box grails

I have problems binding a Boolean property in association classes. The property is true if I check (good), but null if checbox is not set.

I know the problem with the HTML checkbox. I know why "_fieldName" is sent in the parameters, but this "_fieldName" does not set my boolean property to false .

class Person{ String title List<Group> groups = new ArrayList() static hasMany = [groups: Groups] } class Group{ String title Boolean isHidden static belongTo = Person } class PersonController{ def form = { def person = new Person() person.groups.add( new Group() ) return ["person": person] } def handleForm = { def person = new Person( params ) println person.groups[0] } } <g:form action="save"> <g:textField name="title" value="${person?.title}" /> <g:textField name="groups[0].title" value="${person?.groups[0]?.title}"/> <g:checkBox name="groups[0].isHidden" value="${person?.groups[0]?.isHidden}" /> <g:submitButton name="save" value="Save" /> </g:form> 

If I check the box:
[isHidden: on, title: a, _isHidden:]
println person.groups [0] // true

If I do not check the box:
[title: a, _isHidden:]
println person.groups [0] // null



Thank you for help
Tom

Sorry, I was looking for this network but did not receive actual information about my problem.

+6
grails
source share
4 answers

I am correcting the flag tag. Thanks to gid help, now it also works with association.

from source:
http://grails.org/doc/latest/ref/Tags/checkBox.html#

  if (value == null) value = false out << "<input type=\"hidden\" name=\"_${name}\" /><input type=\"checkbox\" name=\"${name}\" " if (value && checked) { out << 'checked="checked" ' } 

in

 if (value == null) value = false def begin = name.lastIndexOf('.') +1 def tail = name.substring( begin); out << "<input type=\"hidden\" name=\"${name.replace( tail, "_" + tail )}\" /><input type=\"checkbox\" name=\"${name}\" " if (value && checked) { out << 'checked="checked" ' } 
+5
source share

After hacking many times, the answer appears that grails is looking for a marker field with the name:

 groups[0]._isHidden 

but not

 _groups[0].isHidden 

which actually generates the g:checkBox . See GrailsDataBinder.java:911 for a confirmation of the StartsWithFieldMarkerPrefix property (PropertyValue pv, String fieldMarkerPrefix)

If you're interested, I uploaded a test project for this gitub.com question

+5
source share

Use the code below,

 <g:checkBox name="checkbox" value="HELLO" /> 

Contact:

+1
source share

Set the default value for the false flag and it should fix the problem. Hope it helps.

0
source share

All Articles