File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Struts: validations using 'validwhen' in validation.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts: validations using Watch "Struts: validations using New topic

Struts: validations using 'validwhen' in validation.xml

Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
In my JSP form , I have two fields: age and term
Now conditions are:
1. age is required
2. age must be an integer
3. age >18 and age <75

4. term is required
5. term must be an integer
6. term < 40 and term >0

7. sum of term and age must be less than 80 , i.e. age+term<80

Since I am using Struts framework, I am writing the validations in the validations.xml . The form bean I am using is DynaValidatorForm.
When I write the xml for all the validations from 1 to 6 (except 7), it works fine. I could also provide client-side validation in the JSP by using the script :

<html:javascript formName="/getQuotes" method="validateForm" dynamicJavascript="true" staticJavascript="true" cdata="false" />

in the <head> portion of the JSP. Now how do I implement the validation for 7 .(sum of term and age must be less than 80 , i.e. age+term<80 ).
Given below is the extract from the validation.xml without the validation for 7.. it works fine.
Here is the xml for validation without validation for 7 but for the other validations:

Now how do I add the validation for 7. I understand I have to use 'validwhen'. I did take help of the tutorial from struts website: though there is example of validwhen but it does not contain any example like the condition in 7 .
I tried by doing this:
<field property="term" depends="required,integer,intRange,validwhen">
key="invalid " resource="false"/>
<arg0 key="QuoteDetailsBean.term" />
<arg1 name="intRange" key="${var:min}" resource="false"/>
<arg2 name="intRange" key="${var:max}" resource="false"/>

<var-value> ((*this* + age) < 80)</var-value>


then when I submit the form, I get this :
javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.NoClassDefFoundError: antlr/TokenStream
java.lang.Class.getDeclaredMethods0(Native Method)

so is the whole thing how I used validwhen wrong? Please let me know how should I do it. Someone told me that validwhen does not support '+','-' i.e. adding of properties. is it so?

I also want the custom message to come from message resource file like say 'registrationForm.invalidAgeTermcombination' .
Can anybody please provide me the xml code to be added to perform validation 7?
[ August 25, 2004: Message edited by: Tanveer Rameez ]

Author of JPhotoBrush Pro (
Martin Podrouzek

Joined: Sep 09, 2004
Posts: 10
Fine day to you Tanveer

I had simmilar problem, and this is what I came up to:

1) I forgot to copy antlr.jar to my lib folder. Make sure you have this file
in your lib folder

2) I moved validwhen to the front of depends statement like this:
as well as <var>...</var> tags
dunno if it has any effect.
this is how it might look:

3) I have tried your code and got several errors "not-well formed". It might be obvious but < (lesser than) is ILLEGAL in XML try using < (ampersand lt semicollumn ) instead.

After I replaced this character it worked fine

The above code is also an example of code for anyone trying to combine mask and validwhen.

Hope it will be of any help


[ September 09, 2004: Message edited by: Martin Podrouzek ]
[ September 09, 2004: Message edited by: Martin Podrouzek ]
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
thanks for the tip.

1) I forgot to copy antlr.jar to my lib folder. Make sure you have this file
in your lib folder

I forgot that i have to add this jar file too. it is not stated in the validation section in the jakarta struts website. thankx for that.
and it worked
Jasmine Wu

Joined: Oct 06, 2004
Posts: 1
I am trying to do this via validator and don't know if it can be done.

I have two fields: "range" and "from". range is a drop down and from is a text field

If range value is equal to "P", I need to check if the from has format of #######.##

But if range is equal to "D", I need to check if what's entered in the from is in the yyyy-yy-yy format.

I am trying to be creative with the validwhen and mask validation rules but I can't seem to come up with something that works.

Please let me know if this is just not possible. If it is, I would appreciate any help.

Martin Podrouzek

Joined: Sep 09, 2004
Posts: 10
Fine day to you Jasmine

frankly, some time ago I came across very same problem. As far as I am concerned using only provided validators

won't work. I'm going to state here how I solved the problem, as well as other possible (hypothetical) solutions.

I haven't used validator in form, you probably seek. The task requires branching and 2 different tests.
If I got it right something like this:

I used server-side validation, and validated input in the Action class.

Struts validator gives you opportunity to create your own validation classes

( .
I haven't tried but after writing your own class you could also add CDATA part to validator-rules.xml file, that

would create client-side validation Javascript. Than, it's up to you how you write the validation classes. I'm going

to write my own classes with such feature. As soon as I'll have something, I'm going to post it here.

Now for the other possible solutions.

First of all it is VERY important to note one phenomena:
Using strings for conditional statements are pain (ie: == 'en'). PLEASE NOTE that this is code used

to generate JAVASCRIPT!
In case of validwhen the generated code will look like this (obviously):

neither single nor double quotes will work.
Unfortunatelly, escaping characters or using character entities won't help either, since validator will add another

backslash resulting in

Does anybody know how to enforce single backslashes?

Following part is for those willing to investigate the problem.

My other ideas:

Idea 1 (Failed):
Use 2 validator tags for one field moving less probable option prior to more probable. If the first option would

fail, second validation would override first.

example 1:

result 1:
Struts will generate 2 validator functions. One function will succeed while other will fail, rendering an error (and

vice versa).

Idea 2 (failed):
Omit mask attribute and implement reg exp. validation in validWhen directly.

example 2:

result 2:
Failure, due to problem with above-described strings.(Using single quotes won't even generate the code) Validator will generate this code:


This way seems to be a dead end.

[ October 09, 2004: Message edited by: Martin Podrouzek ]
Ashwin Puthran

Joined: Mar 25, 2009
Posts: 6
5 yrs later now i am facing the same problem.
Has some one got a solution to this Dead end
I agree. Here's the link:
subject: Struts: validations using 'validwhen' in validation.xml
jQuery in Action, 3rd edition