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]
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.
Joined: Feb 15, 2005
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.
Joined: Aug 25, 2005
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
Joined: Feb 15, 2005
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.
Joined: Aug 25, 2005
Thanks Merill ! mkar Patkar.
subject: How to perform validation conditionally ?