Michael Borgwardt
Michael Borgwardt

Reputation: 346317

Grails: checkbox not being set back to false

I am developing a Grails (1.0.4) app where I want to edit a collection of collections on a single page in a grid view. I got it to work quite well depending only on the indexed parameter handling of Spring MVC, except for one thing:

boolean (or, for that matter, Boolean) values in the grid can be set via checkbox, but not unset, i.e. when I check the checkbox and update, the value is set to true, but afterwards when I edit again, uncheck the checkbox and update, it remains true.

This is the GSP code of the checkbox:

<g:checkBox name="tage[${indexTag}].zuweisungen[${indexMitarb}].fixiert" value="${z.fixiert}" />

And this is the HTML that is generated:

<input type="hidden" name="tage[0].zuweisungen[0]._fixiert" />
<input type="checkbox" name="tage[0].zuweisungen[0].fixiert" checked="checked" id="tage[0].zuweisungen[0].fixiert"  />

I've found a Grails bug that describes exactly this effect, but it's marked as fixed in 1.0.2, and the problem mechanism described there (underscore in hidden field name is put in the wrong place) is not present in my case.

Any ideas what could be the reason?

Upvotes: 9

Views: 6486

Answers (5)

Michael Borgwardt
Michael Borgwardt

Reputation: 346317

This is the solution a guy named Julius Huang proposed on the grails-user mailing list. It's reusable but relies on JavaScript to populate a hidden field with the "false" response for an unchecked checkbox that HTML unfortunately does not send.

I hack GSP to send "false" when uncheck the box (true -> false) with custom TagLib.

By default checkBox send nothing when uncheck, so I use the checkBox as event handler but send hidden field instead.

"params" in Controller can handle "false" -> "true" without any modification. eg. Everything remain same in Controller.

The Custom Tag Usage in GSP (sample usedfunc_F is "true"),

<jh:checkBox name="surveyList[${i}].usedfunc_F" value="${survey.usedfunc_F}"></jh:checkBox>

Here is what the Tag generate,

<input type="hidden" name="surveyList[#{i}].usedfunc_F" id="surveyList[#{i}].usedfunc_F" value="false" />
<input type="checkbox" onclick="jhtoggle('surveyList[#{i}].usedfunc_F')" checked="checked" />

The Javascript

<script type="text/javascript">
function jhtoggle(obj) {
   var jht = document.getElementById(obj);
   jht.value = (jht.value !='true' ? 'true' : 'false');
}
</script>

Upvotes: 4

Michael Borgwardt
Michael Borgwardt

Reputation: 346317

This is my own solution, basically a workaround that manually does what the grails data binding should be doing (but doesn't):

Map<String,String> checkboxes = params.findAll{def i = it.key.endsWith("._fixiert")} // all checkboxes
checkboxes.each{
    String key = it.key.substring(0, it.key.indexOf("._fixiert"))
    int tagIdx = Integer.parseInt(key.substring(key.indexOf('[')+1, key.indexOf(']')))
    int zuwIdx = Integer.parseInt(key.substring(key.lastIndexOf('[')+1, key.lastIndexOf(']')))
    if(params.get(key+".fixiert"))
    {
        dienstplanInstance.tage[tagIdx].zuweisungen[zuwIdx].fixiert = true
    }
    else
    {
        dienstplanInstance.tage[tagIdx].zuweisungen[zuwIdx].fixiert = false                    
    }
}

Works, requires no change in grails itself, but isn't reusable (probably could be made so with some extra work).

Upvotes: 2

Kuukage
Kuukage

Reputation: 173

Try this out, set the logs to DEBUG, frist try the first 3 if they don't show the problem up, flip them all to DEBUG:

codehaus.groovy.grails.web.servlet="error"  //  controllers
codehaus.groovy.grails.web.pages="error" //  GSP
codehaus.groovy.grails.web.sitemesh="error" //  layouts
codehaus.groovy.grails."web.mapping.filter"="error" // URL mapping
codehaus.groovy.grails."web.mapping"="error" // URL mapping
codehaus.groovy.grails.commons="info" // core / classloading
codehaus.groovy.grails.plugins="error" // plugins
codehaus.groovy.grails.orm.hibernate="error" // hibernate integration

This should allow you to see exactly when and how the parameters setting is failing and probably figure out a work around.

Upvotes: 0

Ben Williams
Ben Williams

Reputation: 2297

I would create a small sample app that demonstrates the problem and attach it to the Grails bug (or create a new one). Someone here may be able to debug your sample app or you'll have shown the bug isn't really fixed.

Upvotes: 1

alexpopescu
alexpopescu

Reputation: 9057

I think that the simplest workaround would be to attach a debugger and see why Grails is failing to populate the value. Considering Grails is open source you'll be able to access the source code and once you figure out the solution for it you can patch your version.

I have also found this other bug GRAILS-2861 which mentions the issue related to binding to booleans (see Marc's comment in the thread). I guess that is exactly the problem you are describing.

Upvotes: 1

Related Questions