You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Harland, David" <Da...@dlapiper.com> on 2005/07/18 16:40:01 UTC

How do you create a Validator rule to compare 3 fields

I have three checkboxes that can all either be unchecked or only one can
be checked.

How do I create a Validator rule for this. I don't think it is possible
to do this neatly as I think you will always get more than one of the
same error message. Can someone please tell me if I am right.

Thanks

Dave.

This e mail is from DLA Piper Rudnick Gray Cary UK LLP.

The contents of this email and any attachments are confidential to the intended recipient. They may not be disclosed to or used by or copied in any way by anyone other than the intended recipient. If this email is received in error, please contact DLA Piper Rudnick Gray Cary UK LLP on +44 (0) 8700 111111 quoting the name of the sender and the email address to which it has been sent and then delete it.

Please note that neither DLA Piper Rudnick Gray Cary UK LLP nor the sender accept any responsibility for viruses and it is your responsibility to scan or otherwise check this email and any attachments. 

DLA Piper Rudnick Gray Cary UK LLP is a limited liability partnership registered in England and Wales (registered number OC307847) which provides services from offices in England, Belgium, Germany and the People's Republic of China.  A list of members is open for inspection at its registered office and principal place of business 3 Noble Street, London EC2V 7EE.  Partner denotes member of a limited liability partnership.

DLA Piper Rudnick Gray Cary UK LLP is regulated by the Law Society and is a member of DLA Piper Rudnick Gray Cary, a global legal services organisation, the members of which are separate and distinct legal entities.  For further information, please refer to www.dlapiper.com.




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


Re: How do you create a Validator rule to compare 3 fields

Posted by Jeff Beal <jb...@gmail.com>.
Have you considered using radio buttons instead of checkboxes?  From a
UI standpoint, I think that would make more sense.

That aside, I do not know of any easy way to do this sort of
validation using the Validator plugin.  I would override the validate
method in my action form for just those properties:

public class CustomForm extends ValidatorActionForm {

// Getters / setters

public ActionErrors validate(mapping, request) {
   ActionErrors errors = super.validate(mapping,request); // this will
use the Validator framework
  if (!checkbox1 && !checkbox2 && !checkbox3) return errors;
  if ((checkbox1 && (checkbox2 || checkbox3)) ||
     (checkbox2 && checkbox3) {
     // add an ActionError to errors here
  }
 return errors;
}

}

-- Jeff

On 7/18/05, Harland, David <Da...@dlapiper.com> wrote:
> I have three checkboxes that can all either be unchecked or only one can
> be checked.
> 
> How do I create a Validator rule for this. I don't think it is possible
> to do this neatly as I think you will always get more than one of the
> same error message. Can someone please tell me if I am right.
> 
> Thanks
> 
> Dave.
> 
> This e mail is from DLA Piper Rudnick Gray Cary UK LLP.
> 
> The contents of this email and any attachments are confidential to the intended recipient. They may not be disclosed to or used by or copied in any way by anyone other than the intended recipient. If this email is received in error, please contact DLA Piper Rudnick Gray Cary UK LLP on +44 (0) 8700 111111 quoting the name of the sender and the email address to which it has been sent and then delete it.
> 
> Please note that neither DLA Piper Rudnick Gray Cary UK LLP nor the sender accept any responsibility for viruses and it is your responsibility to scan or otherwise check this email and any attachments.
> 
> DLA Piper Rudnick Gray Cary UK LLP is a limited liability partnership registered in England and Wales (registered number OC307847) which provides services from offices in England, Belgium, Germany and the People's Republic of China.  A list of members is open for inspection at its registered office and principal place of business 3 Noble Street, London EC2V 7EE.  Partner denotes member of a limited liability partnership.
> 
> DLA Piper Rudnick Gray Cary UK LLP is regulated by the Law Society and is a member of DLA Piper Rudnick Gray Cary, a global legal services organisation, the members of which are separate and distinct legal entities.  For further information, please refer to www.dlapiper.com.
> 
> 
> 
> 
> ---------------------------------------------------------------------
> 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: How do you create a Validator rule to compare 3 fields

Posted by Joe Germuska <Jo...@Germuska.com>.
At 1:36 PM -0400 7/18/05, Laurie Harper wrote:
>Harland, David wrote:
>>I have three checkboxes that can all either be unchecked or only one can
>>be checked.
>>
>>How do I create a Validator rule for this. I don't think it is possible
>>to do this neatly as I think you will always get more than one of the
>>same error message. Can someone please tell me if I am right.
>
>You may be able to do what you want with validwhen, though it might 
>get a bit hairy. If only one checkbox should be selected, maybe you 
>could use radio buttons instead? Failing that, you may need to plug 
>in your own custom validator (see [1] for details).

There is an open Bugzilla ticket which offers a powerful "expression" 
validator to Struts; it was written by one of my colleagues, and I 
think it's pretty hot stuff.  There are some discussions underway 
about the best way to add it to Struts right now, but in the 
meantime, if you were so motivated, you could pick your way through 
the ticket and apply the patches to your own project.

It works well and once you have it implemented, it's quite 
straightforward (much more so, in my opinion, than "validwhen" or 
"requiredif").  If you just create all the specified new classes in 
your project's own package space and add dependencies to a few 
things, you should be all set.

See http://issues.apache.org/bugzilla/show_bug.cgi?id=34849

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"Narrow minds are weapons made for mass destruction"  -The Ex

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


Re: How do you create a Validator rule to compare 3 fields

Posted by Laurie Harper <la...@holoweb.net>.
Harland, David wrote:
> I have three checkboxes that can all either be unchecked or only one can
> be checked.
> 
> How do I create a Validator rule for this. I don't think it is possible
> to do this neatly as I think you will always get more than one of the
> same error message. Can someone please tell me if I am right.

You may be able to do what you want with validwhen, though it might get a 
bit hairy. If only one checkbox should be selected, maybe you could use 
radio buttons instead? Failing that, you may need to plug in your own 
custom validator (see [1] for details).


[1] http://struts.apache.org/userGuide/dev_validator.html

> Thanks
> 
> Dave.
> 
> This e mail is from DLA Piper Rudnick Gray Cary UK LLP.
> [... ad nauseum]

Sigh.

L.

-- 
Laurie, Open Source advocate, Java geek and novice blogger:
http://www.holoweb.net/laurie


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


Re: How do you create a Validator rule to compare 3 fields

Posted by Glen Mazza <gr...@verizon.net>.
Pardon the non-answer (and I would encourage anyone who knows how to 
validate interdependent checkboxes to respond), but ordinarily using 
four radio buttons (the fourth being a "none" radio button, selected by 
default) would be the more standard GUI choice, and one for which you 
can forgo validation.

Still, if you go with validating & returning errors on checkboxes, note 
that checkbox state retention issues may need to be tended to, as 
described from page 123 of the Struts Survival Guide[1].

Glen

[1] http://www.objectsource.com/Struts_Survival_Guide.pdf



Harland, David wrote:

> I have three checkboxes that can all either be unchecked or only one can
> be checked.
> 
> How do I create a Validator rule for this. I don't think it is possible
> to do this neatly as I think you will always get more than one of the
> same error message. Can someone please tell me if I am right.
> 
> Thanks
> 
> Dave.
> 
> This e mail is from DLA Piper Rudnick Gray Cary UK LLP.
> 
> The contents of this email and any attachments are confidential to the intended recipient. They may not be disclosed to or used by or copied in any way by anyone other than the intended recipient. If this email is received in error, please contact DLA Piper Rudnick Gray Cary UK LLP on +44 (0) 8700 111111 quoting the name of the sender and the email address to which it has been sent and then delete it.
> 
> Please note that neither DLA Piper Rudnick Gray Cary UK LLP nor the sender accept any responsibility for viruses and it is your responsibility to scan or otherwise check this email and any attachments. 
> 
> DLA Piper Rudnick Gray Cary UK LLP is a limited liability partnership registered in England and Wales (registered number OC307847) which provides services from offices in England, Belgium, Germany and the People's Republic of China.  A list of members is open for inspection at its registered office and principal place of business 3 Noble Street, London EC2V 7EE.  Partner denotes member of a limited liability partnership.
> 
> DLA Piper Rudnick Gray Cary UK LLP is regulated by the Law Society and is a member of DLA Piper Rudnick Gray Cary, a global legal services organisation, the members of which are separate and distinct legal entities.  For further information, please refer to www.dlapiper.com.
> 
> 
> 
> 
> ---------------------------------------------------------------------
> 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