Discussion:
Checkbox not being set back to false
Michael Borgwardt
2009-02-27 22:53:54 UTC
Permalink
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 standard grails magic (which is, I believe, provided
by Spring MVC) using indexed parameters, 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" />

The controller's update action is provided by dynamic scaffolding.

I've found a Grails bug that describes exactly this effect:
http://jira.codehaus.org/browse/GRAILS-2486

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? Is it the nested indexing?

Thanks,
Michael
adwin grails
2009-02-28 01:45:54 UTC
Permalink
I think you shouldn't put value on checkbox, when your value is empty,
then it will send as empty string. instead use "checked". I got a
confusion as well with this. But I solve the problem by not using
value.

regards
adwin
Post by Michael Borgwardt
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 standard grails magic (which is, I believe, provided
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.
    <g:checkBox name="tage[${indexTag}].zuweisungen[${indexMitarb}].fixiert"
value="${z.fixiert}" />
    <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"  />
The controller's update action is provided by dynamic scaffolding.
http://jira.codehaus.org/browse/GRAILS-2486
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? Is it the nested indexing?
Thanks,
Michael
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Michael Borgwardt
2009-02-28 09:11:35 UTC
Permalink
This post might be inappropriate. Click to display it.
adwin grails
2009-02-28 09:23:17 UTC
Permalink
Yes, it should work, but I don't know why it doesnt work. If we use
scaffold, it use value instead checked. But they store using
domain.properties = params .. and I thinks there are special process
when you assign properties with params.

you can try :

<g:checkbox .... value="something_in_string" />

and when you checked it, it will return "something_in_string" instead
of true or false. the default is "on".

I never using checkbox anymore, for true false / boolean, I simply
using g:select with true or false. :)

I think grails development team should fix checkbox behaviour ;)


regards
adwin
Post by Michael Borgwardt
Post by adwin grails
I think you shouldn't put value on checkbox, when your value is empty,
then it will send as empty string. instead use "checked". I got a
confusion as well with this. But I solve the problem by not using
value.
<g:checkBox name="tage[${indexTag}].zuweisungen[${indexMitarb}].fixiert"
checked="${z.fixiert}"/>
Then the checkbox doesn't show up as checked even when it should, and
if I submit it unchecked, it does not set the domain value to false
either. Besides, the taglib documentation for "checked" says
"The checkBox tag typically only requires the name and value
attributes and will infer whether the checkbox should be checked or
not from the value. However, if you need to override this behaviour
the checked attribute can be used for fine grained control."
I don't think this means that "checked" helps with the data binding
back to the domain model, only with determining whether it should be
displayed as checked in the HTML - which is working fine for me
without using "checked".
Meanwhile, I have achieved the desired result by "manually" going
through the params in the update action and mapping the checkboxes.
But I don't like doing that, as it violates DRY and mapping the data
SHOULD work without it.
Thanks,
Michael
---------------------------------------------------------------------
   http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Julius Huang
2009-02-28 12:59:24 UTC
Permalink
This post might be inappropriate. Click to display it.
Michael Borgwardt
2009-03-01 12:56:49 UTC
Permalink
Post by Julius Huang
I had the same problem with "Edit List in Lists" just two days ago.
params + g:checkBox does not work when the property is boolean in Lists.
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.
Interesting approach, especially since the taglib makes it reusable everywhere.
Post by Julius Huang
BTW, the reason I use this approach is I don't know
how the g:checkBox generate _hiddenfield work with params in controller or
how to make it work.
Well, this is my solution that works purely inside the controller
(uses unmodified grails taglib):

Map<String,String> checkboxes = params.findAll{def i =
it.key.endsWith("._fixiert")}
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
}
}

Would take some work to make it reusable, but I think I'll stick with
it, since I don't like having to rely on JavaScript for basic app
functionality.

Regards,
Michael

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Julius Huang
2009-03-01 15:49:35 UTC
Permalink
Hi,

On 01 Mar , 2009, at 20:56 , Michael Borgwardt wrote:
.....
.....
Post by Michael Borgwardt
Well, this is my solution that works purely inside the controller
Map<String,String> checkboxes = params.findAll{def i =
it.key.endsWith("._fixiert")}
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
}
}
I think "params -> domain class" did something like that behind the
scene,
but seems not working with Collections or Lists (I use LazyList).
Is it possible to make params works like the controller solution above?

I am new to Java and Grails.

kind of lost when looking at
"src/web/org/codehaus/groovy/grails/web/servlet/
GrailsRequestContext.java".
Is it the right place to look?
If not, can anyone suggest where I should look?
I can try to make the params work with Boolean in "Collections and
Lists".
Post by Michael Borgwardt
Would take some work to make it reusable, but I think I'll stick with
it, since I don't like having to rely on JavaScript for basic app
functionality.
I agree with you on not using JavaScript in this case.
Modify TagLib + JavaScript is easier for me.

Regards,
Julius
Post by Michael Borgwardt
Regards,
Michael
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...