Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

validation of a field depending on the value of another field

 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am using Struts1.1(including validation framework) & weblogic 8.1. My validation rules for a particular variable depends on the value of another variable. For example, if I am validating a zip code variable, it can be a US zip or any other country's zip code. The rules are differnt, but I can tell if it's US zip code or international using another field. I tried using requiredif, but that did not work. I appreciate any suggestions. I would upgrade struts if the new version offers a solution.
Thanks in advance,
Sai
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sai kinnera:
I would upgrade struts if the new version offers a solution.


Get ready to upgrade. The latest version (Or any 1.2 version) offers a "validWhen" vaidation rule that you may be able to use. It's much more flexible than requiredIf. I will warn you, though, that it doesn't handle really complex situations. It's possible that even with the newest version you will end up writing a custom validation rule in order to meet your needs.
 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Merrill,
Thanks for the reply.I have gon through the documentation for validwhen. Even this compares if the value of one variable is not null
or should not exceed a limit, then another variable can not be null. But my requirement is, if one variable has
a specific value, the second variable should follow a different rule to validate that field. To sum up, I am
going to write a validator for the zip code validation, and I should be able to read the value of another variable,
say, location(US/ International). How do I read the value of location in the validator that I write for zip code.

Thanks,
Sai
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I warned, vaidWhen is not a fix-all for everything. It sounds like your requirement is complex enough that it may not work for you.

As I see it, here are your options:

1-Create a custom validation rule that will handle your situation and use it within the Vaidation Framework.

2-Abandon use of the Struts Validator Framwork, and just validate based using the validate() method of your ActionForm.

3-Use a mixture of the Validation Framework and custom validation. You can do this by putting logic that is too complex for the validation framework in a validate() method, and then calling super.validate() within that method to cause the Validation Framework validations to be performed.
 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Merrill,
I am writing a custom validator. In that method, there will be two kinds of validations 1. if the country(some other field) is US 2. if it's not US. So, my method signature: public static boolean validateZipCode(Object bean,Field field). The field is the zip code field from validation.xml file. Within this method, how do I check for the country whethter it's US or not. How do I read the value of country field in this method.
Thanks,
Sai
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest you study the section entitled "Pluggable Validators" in this link.

In the link, they show you a sample custom validation rule for verifying that two fields both have the same value. I'd suggest you study the sample code and retrieve your country field the same way they retrieve their "secondProperty" field.

Here's a bonus: If you look in the section entitled "Constants/Variables", you will find a regex expression for a valid US zip code.
[ August 07, 2006: Message edited by: Merrill Higginson ]
 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Merrill. This seems to work forme .So,I will upgrade to struts1.2.9 and test it.
Sai
 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merrill just to let you know that this technique is working even with struts1.1, if I write a custom validator. Thanks a lot.
Sai
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!
Glad to hear you got it working. Next time someone asks a question about a custom validation rule, I'll send them to you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic