This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Struts and the fly likes How to perform validation conditionally ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to perform validation conditionally ?" Watch "How to perform validation conditionally ?" New topic
Author

How to perform validation conditionally ?

omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
Hi guys,

The validation framework provided by struts is really great ... but i am facing certain problems in certain scenarios like this :-

1) On my jsp page, i dispaly certain fields based on upon certain conditions like the user is an admin or not ...etc., In such a case, my "required" validation defined for that field in the validation framework must not be fire ! .... But then how do we conditionally control this struts 1.2.8 ?

2) Certain fields must be filled only if a particular field is not empty ! For example :-
If AT ALL existing password field is displayed only then, the new and confirm password must be filled! else they are optional !... Can such conditional validation be used in the validation.xml ?

Please, some guide me ! .... ...i am really stuck up at this point since a long time and not able to figure out a work around.

Please Merill ...do you have any idea how this can be done ?

Thanks and Regards
mkar Patkar


Thanks and Regards
Omkar Patkar (SCJP 1.4)[url]http://javacollectionsnotes.blogspot.com[/url] | [url]http://omkar-myscjpexp.blogspot.com[/url]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I believe at least some of what you want to do can be done by using the "validWhen" rule. Please read the section entitled "Designing Complex Validations with validWhen" in this link.

If you can't make it work with the standard validations, you always have the option of creating your own custom validation rule. The above link explains how to do that as well.


Merrill
Consultant, Sima Solutions
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
Hi Merill,

Nice link! , but its like ... A jsp form is associated with a form bean and i think the validator framework uses the form bean not the jsp page field. So even if the field is not displayed on the screen, the corresponding formbean property is used by the framework and error message will be thrown.

I mean lets say for example, username field is not displayed on the screen. But when the, form-bean is created at that time its properties will get default values. So username being a string will get default value of null. And if in the validation.xml i have put a "validwhen" or "required" check, if userName is null or not then the test will be true and it will display an error. So, end user will get a feel that while filling the form on the jsp page, he/she did not see the username field and after clicking on submit an error message is shown :-

"username is required." which is not correct ! ...So how can we handle such a scenario ?

Thanks and regards
Omkar Patkar.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's an example:

Suppose you have a property on your ActionForm named "admin" that is a boolean and you have included it on your page as a hidden field. If admin is true, the userName field is not displayed, so it does not need to be validated. If admin is false, the userName field is displayed and does need to be validated. Here's what you'd put in your validation.xml file:

The above logic is saying that if admin is true, consider the field to be valid whether it's null or not, but if admin is not true, consider the field to be valid only if it is not null.
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
ok .... But that would be the case when the field is hidden. What if the field is not rendered at all as per some condition ? ... Then, in this case when the field is not displayed, how can we ask other dependent fields to skip their dependency on that field ? Or, how can we ask Validator framework not to validate a property of form bean for "required" condition, if it is not rendered in the final html ? ...is their any facility to check this in Struts 1.2.8 ?

Thanks and Regards
Omkar Patkar
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by omkar patkar:
Or, how can we ask Validator framework not to validate a property of form bean for "required" condition, if it is not rendered in the final html ? ...is their any facility to check this in Struts 1.2.8 ?

Sorry. To my knowledge there is no such facility.

To be honest, even though I've become familiar with Struts Validation in order to help others with it, I don't use it in my own applications. By writing my own validation code, I have complete control over what gets validated and when.

You may want to consider not using the Validation framework, at least for this particular page. If you want to keep the same look and feel, you can just override the validate method of your ActionForm and put your validation logic in it.

If it's really important to you to stay within the Struts Validation framework, you can always write your own validation rule.

I've shown you what's avaiable. Now it's up to you to figure out what to do with it.
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
ok. Currently what i am doing is i have written manually javascript for such fields. and struts validation for other normal independent fields which are always dislayed. ... But i must say the discussion was interesting merill.

Thanks Merill !
mkar Patkar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to perform validation conditionally ?
 
Similar Threads
Validating dynamic variables
Validator..How to stop the validation after the first field?
Validation in Struts 2
Help with simple validation problem in Struts
STRUTS VALIDATOR FRAMEWORK