wood burning stoves 2.0*
The moose likes Struts and the fly likes validation of a field depending on the value of another field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validation of a field depending on the value of another field" Watch "validation of a field depending on the value of another field" New topic
Author

validation of a field depending on the value of another field

sai kinnera
Ranch Hand

Joined: Aug 12, 2004
Posts: 52
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

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
sai kinnera
Ranch Hand

Joined: Aug 12, 2004
Posts: 52
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Aug 12, 2004
Posts: 52
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Aug 12, 2004
Posts: 52
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

Joined: Aug 12, 2004
Posts: 52
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

Joined: Feb 15, 2005
Posts: 4864
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.
 
Consider Paul's rocket mass heater.
 
subject: validation of a field depending on the value of another field
 
Similar Threads
How to make required validation check dependent on another value?
How javascript variable assigned to JSP variable
Is Backing bean Required
jQuery-How to highlight invalid input field value after getting validation error
Java Validation! Field should be Integer