You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Peter L. Berghold" <pe...@berghold.net> on 2008/01/02 02:02:47 UTC

unexpected behavior with a checkbox.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi folks,

I have a form that the values for each field is populated as a result of
a database query.  One of the fields on the form is rendered as a
checkbox and the corresponding table column is a boolean.

When I pull in a "true" value from the database the reset() method of my
form sets the checkbox accordingly.

When the page is rendered the checkbox is set the way I expect.  So far,
so good.

Where the trouble comes in is if the field is unchecked which I thought
would set the value of the Boolean to "false."  It doesn't, it remains
as a true.

How do I implement behavior where if the checkbox is unchecked it sets
the corresponding property of the form to false?

- ----

Peter L. Berghold                   Unix Professional
Peter@Berghold.Net             AIM: redcowdawg YIM: blue_cowdawg
"Those who fail to learn from history are condemned to repeat it."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHeuK3UM9/01RIhaARAkOwAJ9uNnENpwIVb9XAl8bR26xKvfEgXwCfSKXM
Dwcd0MqGUCQ2O09XMgP+TMk=
=Qbl6
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unexpected behavior with a checkbox.

Posted by "David Durham, Jr." <da...@gmail.com>.
On Jan 2, 2008 10:59 PM, ravi_eze <ra...@yahoo.com> wrote:
>
> hi,
>
> the workaround works, but causes problems if the checkbox should be
> validated. More over the soln is based on assumptions that need not hold
> always.

Which solution are you saying is based on assumptions that need not
hold, and why?  I think you'll find that my solution wherein you 1)
you don't use session-scoped action forms (or the equivalent in s2)
and 2) you don't initialize checkboxes in action form initialization,
covers all bases, including validation.  Really, it's the only viable
option for struts 1, that I know of.  If the assumption that doesn't
hold is that you need session-scoped-like form behavior, then that's
easily resolved.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unexpected behavior with a checkbox.

Posted by ravi_eze <ra...@yahoo.com>.
hi,

the workaround works, but causes problems if the checkbox should be
validated. More over the soln is based on assumptions that need not hold
always. There are 2 possible soln for this in my view:

1. use checkbox interceptor
(http://struts.apache.org/2.x/docs/checkbox-interceptor.html). But i found
that this doesnt work if u are using <s:checkboxList ..> the reson being
that the corresponding hidden text-field ("__checkbox_<checkboxListName>)" 
is not produced. (may be u can try creating this hidden field). Now use
checkbox interceptor before param interceptor of ur config xml file.

...
<interceptor-ref name="conversionError" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="params" /> 
...

2. Create a reset interceptor!: now what is this? Write a custom interceptor
(http://struts.apache.org/2.x/docs/writing-interceptors.html) which would
reset all the attributes/ field/ only chcek-box-field variables of the
action class. Place this interceptor as below: 
...
<interceptor-ref name="resetInterceptor">
  checkboxListvariable
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="params" />
...

Now what happens is: always before the paramas are loaded from the request
they are all reset to null/ what ever. Now if the params do not have the
checkbox field in it then the corresponding variables are not touched; this
way ur work is achieved. This would fire validations also. I am currently
using this approache and find it very useful. 

Hope this helps :)

cheers,
Ravi 



David Durham, Jr. wrote:
> 
>> >
>> >    request.getParameter("myUncheckedCheckbox") == null
>> >
>>
>> Oh.. yuck.. but thanks David for that work around.  Tried it and it
>> works for my purposes.
> 
> There are things that can be done to avoid to the ugly null check.
> 
> 1. Don't initialize check-box fields.  No initial values.
> 2. Don't use session-scoped forms.
> 
> It sounds like you need to start with a blank or default form that has
> a check-box checked.  Perhaps the user should receive spam?  :)  I
> haven't thought about that case much, but it seems similar in that, if
> you don't want to use an initial value for reasons previously
> mentioned, you're faced with using some other means to indicate this
> box is checked by default.  If you have a "display new form action,"
> you can put a value on the form whereby the box will start out
> checked.  This is different than defining an initial value.   So long
> as you don't specify an initial value for check-box fields, and Struts
> creates a new form object for each submission, the state of a
> check-box is preserved within an ActionForm.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/unexpected-behavior-with-a-checkbox.-tp14572206p14591715.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unexpected behavior with a checkbox.

Posted by "David Durham, Jr." <da...@gmail.com>.
> >
> >    request.getParameter("myUncheckedCheckbox") == null
> >
>
> Oh.. yuck.. but thanks David for that work around.  Tried it and it
> works for my purposes.

There are things that can be done to avoid to the ugly null check.

1. Don't initialize check-box fields.  No initial values.
2. Don't use session-scoped forms.

It sounds like you need to start with a blank or default form that has
a check-box checked.  Perhaps the user should receive spam?  :)  I
haven't thought about that case much, but it seems similar in that, if
you don't want to use an initial value for reasons previously
mentioned, you're faced with using some other means to indicate this
box is checked by default.  If you have a "display new form action,"
you can put a value on the form whereby the box will start out
checked.  This is different than defining an initial value.   So long
as you don't specify an initial value for check-box fields, and Struts
creates a new form object for each submission, the state of a
check-box is preserved within an ActionForm.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unexpected behavior with a checkbox.

Posted by "Peter L. Berghold" <pe...@berghold.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David Durham, Jr. wrote:

> 
>    request.getParameter("myUncheckedCheckbox") == null
> 



Oh.. yuck.. but thanks David for that work around.  Tried it and it
works for my purposes.

- ----

Peter L. Berghold                   Unix Professional
Peter@Berghold.Net             AIM: redcowdawg YIM: blue_cowdawg
"Those who fail to learn from history are condemned to repeat it."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHe+S6UM9/01RIhaARAs8ZAJ42nyChc1WFlTvEDTz9rWc2QOKG3gCdGzlj
wCXoRk5rrV7ZbU8JG/who1E=
=R9NI
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unexpected behavior with a checkbox.

Posted by "David Durham, Jr." <da...@gmail.com>.
> > The issue is that an unchecked checkbox does not send a value when a
> > form is submitted, so:
>
> This depends on (a) whether or not we're talking about S2 and (b) if we are
> whether or not we're using an interceptor stack that includes the "checkbox"
> interceptor.

I just meant that an HTTP Server won't see a request parameter for an
unchecked checkbox [sic], because a W3C conforming HTML browser won't
send one [request parameter].  The checkbox interceptor seems kludgey
to me, but if it solves this problem for people in a mostly
transparent way, then it's a good thing.


-Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unexpected behavior with a checkbox.

Posted by Dave Newton <ne...@yahoo.com>.
--- "David Durham, Jr." wrote:
> Peter L. Berghold wrote:
>> Where the trouble comes in is if the field is unchecked which I thought
>> would set the value of the Boolean to "false."  It doesn't, it remains
>> as a true.
>>
>> How do I implement behavior where if the checkbox is unchecked it sets
>> the corresponding property of the form to false?
> 
> The issue is that an unchecked checkbox does not send a value when a
> form is submitted, so:

This depends on (a) whether or not we're talking about S2 and (b) if we are
whether or not we're using an interceptor stack that includes the "checkbox"
interceptor.

d.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unexpected behavior with a checkbox.

Posted by "David Durham, Jr." <da...@gmail.com>.
> Where the trouble comes in is if the field is unchecked which I thought
> would set the value of the Boolean to "false."  It doesn't, it remains
> as a true.
>
> How do I implement behavior where if the checkbox is unchecked it sets
> the corresponding property of the form to false?

The issue is that an unchecked checkbox does not send a value when a
form is submitted, so:

   request.getParameter("myUncheckedCheckbox") == null

You can run this check in an action, and modify forms appropriately.
I can say more on this topic, if you need, wrt. session-scoped forms
(and why you should not use them).

-Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org