Here is some code that I submitted to the dev list a while back. It
might be useful as it solves the problem of the 'unchecked' checkbox
submit.  Hope it helps.

Laine

-----------------------------------------

In order to capture the act of a user unchecking checkboxes and to
initialize the checkbox as checked/unchecked with non boolean values I
made a couple of changes to the
org.apache.struts.taglib.html.CheckboxTag.  

Basically the changes consisted of:

1) Using the supplied value attribute to match against the bean property
value and generating the 'checked' attribute accordingly.  If the value
is not supplied then the previously implemented method of checking 'on',
'true', or 'yes' is performed.  So I do not think that default behavior
should be affected.

2) Added support for two new attributes - submitUnchecked (boolean) and
uncheckedValue (String).  Neither of these attributes are required and
the submitUnchecked defaults to 'false'.  If a user chooses to submit
unchecked values then two form inputs will be generated that facilitate
this function.  See below for an example:

Tag notation -
<html:checkbox  name="bean" property="indexedBean.property"
submitUnchecked="true" uncheckedValue="0" value="1"/>

Generated HTML -
<input type="checkbox" name="indexedBean.property_checkBox" value="1" 
        
onclick="(this.checked?this.form['indexedBean.property'].value='1':this.
form['indexedBean.property'].value='0')">
<input type="hidden" name="indexedBean.property" value="0"/> (where 0 is
the current value of the bean property)

So in this scenario the actual checkbox param in the request will be
ignored and the hidden input will be used to maintain the bean property
that the user wishes.

In the case where the user does not provide the two new attributes the
html will be generated as it is today with the exception of the
determination of the checked or unchecked mentioned above.

I could not see how this functionality was present in the current
checkbox tag without explicitly declaring two tags and adding javascript
to the checkbox tag.  Even then some scripting may be required to
differentiate between the two inputs and to initialize the hidden one.
If I missed something could someone point me in the right direction.  I
have attached the CheckboxTag.java file for review and use if anyone is
interested.
 
Thanks.  Comments would be appreciated.

Laine



-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, May 08, 2001 5:12 PM
To: [EMAIL PROTECTED]
Subject: Re: Unchecking radio buttons?




thanks for the replies.

Yes, I need the checkbox capabilities, not radio buttons.

Question with these, though - is there an equivalent to the "checked"
attribute
in HTML reference speck?

I am creating checkbox with the following:  <html:checkbox
property="notFolders"
value="true"/>, and underlying value of notFolders is "false".  Would
therefore
expect checkbox to appear as unchecked, and True to be submitted is
checkbox is
clicked.

Showing up as already checked, however.

Would appreciate any help...

Dave



Reply via email to