• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How to perform validation conditionally ?

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic