You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Laurie Harper <la...@holoweb.net> on 2005/11/15 00:54:23 UTC

Re: Validator - how to check a checkbox

David Bolsover wrote:
> Hi all
> 
> I've done a deal of searching through the archives, looked at the examples -
> even read the docs - I'm still stuggling with this...
> 
> I need to validate a number of fields in a from; certain fields are required
> if a checkbox is checked and should be empty if the checkbox is not checked.
> 
> What is the correct syntax in validator.xml?
> 
> 'date_Shipped' must only be populated if the 'complete' checkbox is checked.
> 
> As an example of what I have tried....broken...
> 
> <field property="date_Shipped" depends="validwhen">
>  <arg0 key="returns.date_Shipped"/>
>    <var>
>     <var-name>test</var-name>
>     <var-value>(complete == 'on')</var-value>
>    </var>
> </field>

That says date_Shipped is valid *only* when complete has the value 'on'. 
And since a checkbox field is never going to have the value 'on', 
date_Shipped isn't ever going to be valid...

What you want is probably something like

    (((complete == false) and (*this* == null)) or
     ((complete == true)  and (*this* != null)))

I'm not sure if true/false is what you need, or if that needs to be a 
test for null; it probably depends on the type you've declared for the 
'complete' property in your form.

L.


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


Re: Validator - how to check a checkbox - solution

Posted by Jeff Beal <jb...@gmail.com>.
On 11/15/05, Michael Jouravlev <jm...@gmail.com> wrote:
> But then you rely on the undocumented feature/gotcha in BeanUtils.
> What if the way the parameters are processed is changed in future
> versions? I think that this approach of setting "false" as default is
> useful not only for Validator usage, but for submitting checkbox
> values in general, it allows not to clear checkbox values in the form
> bean each time before submission.
>
> Should BeanUtils team clearly document that only first parameter of
> the same name is accepted?

This is the behavior of request.getParameter(), so I don't think
there's much risk with this ever changing, or much need for BeanUtils
to document it.

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


Re: Validator - how to check a checkbox - solution

Posted by Michael Jouravlev <jm...@gmail.com>.
This is an interesting hack. Indeed, HTML 4.01 specifies that when
application/x-www-form-urlencoded content is submitted, "the control
names/values are listed in the order they appear in the document". [1]
So, the strict argument order should be pretty much guaranteed by
compliant browsers.

But then you rely on the undocumented feature/gotcha in BeanUtils.
What if the way the parameters are processed is changed in future
versions? I think that this approach of setting "false" as default is
useful not only for Validator usage, but for submitting checkbox
values in general, it allows not to clear checkbox values in the form
bean each time before submission.

Should BeanUtils team clearly document that only first parameter of
the same name is accepted?

[1] http://www.w3.org/TR/REC-html40/interact/forms.html

Michael.

On 11/15/05, David Bolsover <st...@bolsover.com> wrote:
>
>
> I finally have a solution that works...
>
>
> Problem
> date_Shipped is a text field backed by a String in the ActionForm
> complete is a checkbox backed by a boolean in the ActionForm
>
> Form validation is required such that an entry in date_Shipped is required
> if complete is true.
>
> validation.xml
> *****************
> <field property="date_Shipped" depends="validwhen">
>  <arg0 key="returns.date_Shipped"/>
>  <var>
>   <var-name>test</var-name>
>   <var-value>
>    (((complete == 'false') and (*this* == null)) or
>    ((complete == 'true')  and (*this* != null)))
>   </var-value>
>  </var>
> </field>
>
>
> Note: 'false', 'true' are quoted.
>
>
> .jsp
> ******************
> <snip>
> <html:checkbox property="complete"/>
> <input type="hidden" name="complete" value="false" />
> </snip>
>
> The key point here is that HTML spec says that form elements must be
> submitted in the GET or POST in the order that they are specified in the
> HTML - so in the above, "complete=true&complete=false" is passed. But the
> beanutils code only uses the first parameter to set the value. If the
> checkbox is left unchecked, then there will be only 1 parameter
> "complete=false" which will ensure the property is set.
>
> My thanks to Chris Felaco for his original post re the above to jGuru.
>
> db
>
>
> > -----Original Message-----
> > From: David Bolsover [mailto:struts-user@bolsover.com]
> > Sent: 15 November 2005 08:25
> > To: 'Struts Users Mailing List'
> > Subject: RE: Validator - how to check a checkbox
> >
> > Hi Laurie
> >
> > No joy with this I'm afraid..
> >
> > Using your suggestion, 'date_Shipped' is always required no
> > matter the state of 'complete'.
> >
> > date_Shipped is a String.
> > complete is a boolean.
> >
> > I had expected that the status of a boolean would be either
> > true or false - but now I'm not sure - also, I'm not sure how
> > to debug this - without digging into the validator code.  I
> > assume that the validator does it's stuff before the
> > ActionForm gets to my Action; returning the user to the input
> > if validation fails.
> >
> > Any more suggestions?
> >
> > db
> >
> >
> > > -----Original Message-----
> > > From: news [mailto:news@sea.gmane.org] On Behalf Of Laurie Harper
> > > Sent: 14 November 2005 23:54
> > > To: user@struts.apache.org
> > > Subject: Re: Validator - how to check a checkbox
> > >
> > > David Bolsover wrote:
> > > > Hi all
> > > >
> > > > I've done a deal of searching through the archives, looked at the
> > > > examples - even read the docs - I'm still stuggling with this...
> > > >
> > > > I need to validate a number of fields in a from; certain
> > fields are
> > > > required if a checkbox is checked and should be empty if
> > > the checkbox is not checked.
> > > >
> > > > What is the correct syntax in validator.xml?
> > > >
> > > > 'date_Shipped' must only be populated if the 'complete'
> > > checkbox is checked.
> > > >
> > > > As an example of what I have tried....broken...
> > > >
> > > > <field property="date_Shipped" depends="validwhen">  <arg0
> > > > key="returns.date_Shipped"/>
> > > >    <var>
> > > >     <var-name>test</var-name>
> > > >     <var-value>(complete == 'on')</var-value>
> > > >    </var>
> > > > </field>
> > >
> > > That says date_Shipped is valid *only* when complete has the value
> > > 'on'.
> > > And since a checkbox field is never going to have the value 'on',
> > > date_Shipped isn't ever going to be valid...
> > >
> > > What you want is probably something like
> > >
> > >     (((complete == false) and (*this* == null)) or
> > >      ((complete == true)  and (*this* != null)))
> > >
> > > I'm not sure if true/false is what you need, or if that
> > needs to be a
> > > test for null; it probably depends on the type you've
> > declared for the
> > > 'complete' property in your form.
> > >
> > > L.

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


Validator - how to check a checkbox - solution

Posted by David Bolsover <st...@bolsover.com>.
 

I finally have a solution that works...


Problem
date_Shipped is a text field backed by a String in the ActionForm
complete is a checkbox backed by a boolean in the ActionForm

Form validation is required such that an entry in date_Shipped is required
if complete is true.

validation.xml
*****************
<field property="date_Shipped" depends="validwhen">
 <arg0 key="returns.date_Shipped"/>
 <var>
  <var-name>test</var-name>
  <var-value>
   (((complete == 'false') and (*this* == null)) or
   ((complete == 'true')  and (*this* != null)))
  </var-value>
 </var>
</field>


Note: 'false', 'true' are quoted.


.jsp
******************
<snip>
<html:checkbox property="complete"/>
<input type="hidden" name="complete" value="false" />
</snip>

The key point here is that HTML spec says that form elements must be
submitted in the GET or POST in the order that they are specified in the
HTML - so in the above, "complete=true&complete=false" is passed. But the
beanutils code only uses the first parameter to set the value. If the
checkbox is left unchecked, then there will be only 1 parameter
"complete=false" which will ensure the property is set.

My thanks to Chris Felaco for his original post re the above to jGuru.

db


> -----Original Message-----
> From: David Bolsover [mailto:struts-user@bolsover.com] 
> Sent: 15 November 2005 08:25
> To: 'Struts Users Mailing List'
> Subject: RE: Validator - how to check a checkbox
> 
> Hi Laurie
> 
> No joy with this I'm afraid..
> 
> Using your suggestion, 'date_Shipped' is always required no 
> matter the state of 'complete'.
> 
> date_Shipped is a String.
> complete is a boolean.
> 
> I had expected that the status of a boolean would be either 
> true or false - but now I'm not sure - also, I'm not sure how 
> to debug this - without digging into the validator code.  I 
> assume that the validator does it's stuff before the 
> ActionForm gets to my Action; returning the user to the input 
> if validation fails.
>  
> Any more suggestions?
> 
> db
> 
> 
> > -----Original Message-----
> > From: news [mailto:news@sea.gmane.org] On Behalf Of Laurie Harper
> > Sent: 14 November 2005 23:54
> > To: user@struts.apache.org
> > Subject: Re: Validator - how to check a checkbox
> > 
> > David Bolsover wrote:
> > > Hi all
> > > 
> > > I've done a deal of searching through the archives, looked at the 
> > > examples - even read the docs - I'm still stuggling with this...
> > > 
> > > I need to validate a number of fields in a from; certain 
> fields are 
> > > required if a checkbox is checked and should be empty if
> > the checkbox is not checked.
> > > 
> > > What is the correct syntax in validator.xml?
> > > 
> > > 'date_Shipped' must only be populated if the 'complete' 
> > checkbox is checked.
> > > 
> > > As an example of what I have tried....broken...
> > > 
> > > <field property="date_Shipped" depends="validwhen">  <arg0 
> > > key="returns.date_Shipped"/>
> > >    <var>
> > >     <var-name>test</var-name>
> > >     <var-value>(complete == 'on')</var-value>
> > >    </var>
> > > </field>
> > 
> > That says date_Shipped is valid *only* when complete has the value 
> > 'on'.
> > And since a checkbox field is never going to have the value 'on', 
> > date_Shipped isn't ever going to be valid...
> > 
> > What you want is probably something like
> > 
> >     (((complete == false) and (*this* == null)) or
> >      ((complete == true)  and (*this* != null)))
> > 
> > I'm not sure if true/false is what you need, or if that 
> needs to be a 
> > test for null; it probably depends on the type you've 
> declared for the 
> > 'complete' property in your form.
> > 
> > L.
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


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


RE: Validator - how to check a checkbox

Posted by David Bolsover <st...@bolsover.com>.
Hi Laurie

No joy with this I'm afraid..

Using your suggestion, 'date_Shipped' is always required no matter the state
of 'complete'.

date_Shipped is a String.
complete is a boolean.

I had expected that the status of a boolean would be either true or false -
but now I'm not sure - also, I'm not sure how to debug this - without
digging into the validator code.  I assume that the validator does it's
stuff before the ActionForm gets to my Action; returning the user to the
input if validation fails.
 
Any more suggestions?

db


> -----Original Message-----
> From: news [mailto:news@sea.gmane.org] On Behalf Of Laurie Harper
> Sent: 14 November 2005 23:54
> To: user@struts.apache.org
> Subject: Re: Validator - how to check a checkbox
> 
> David Bolsover wrote:
> > Hi all
> > 
> > I've done a deal of searching through the archives, looked at the 
> > examples - even read the docs - I'm still stuggling with this...
> > 
> > I need to validate a number of fields in a from; certain fields are 
> > required if a checkbox is checked and should be empty if 
> the checkbox is not checked.
> > 
> > What is the correct syntax in validator.xml?
> > 
> > 'date_Shipped' must only be populated if the 'complete' 
> checkbox is checked.
> > 
> > As an example of what I have tried....broken...
> > 
> > <field property="date_Shipped" depends="validwhen">  <arg0 
> > key="returns.date_Shipped"/>
> >    <var>
> >     <var-name>test</var-name>
> >     <var-value>(complete == 'on')</var-value>
> >    </var>
> > </field>
> 
> That says date_Shipped is valid *only* when complete has the 
> value 'on'. 
> And since a checkbox field is never going to have the value 
> 'on', date_Shipped isn't ever going to be valid...
> 
> What you want is probably something like
> 
>     (((complete == false) and (*this* == null)) or
>      ((complete == true)  and (*this* != null)))
> 
> I'm not sure if true/false is what you need, or if that needs 
> to be a test for null; it probably depends on the type you've 
> declared for the 'complete' property in your form.
> 
> L.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


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