Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Struts: validations using 'validwhen' in validation.xml

843836Aug 25 2004 — edited Apr 25 2007
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.
.
<formset>
 <form name="/getQuotes">
......
<field property="age" depends="required,integer,intRange">
	<arg0 key="QuoteDetailsBean.age1" />
	<arg1 name="intRange" key="${var:min}" resource="false"/>
	<arg2 name="intRange" key="${var:max}" resource="false"/>
	<var><var-name>min</var-name><var-value>18</var-value>  </var>
	<var><var-name>max</var-name><var-value>75</var-value> </var>
</field>
<field property="term" depends="required,integer,intRange">
	<arg0 key="QuoteDetailsBean.term" />
	<arg1 name="intRange" key="${var:min}" resource="false"/>
	<arg2 name="intRange" key="${var:max}" resource="false"/>
	<var><var-name>min</var-name><var-value>0</var-value></var>
	<var><var-name>max</var-name><var-value>40</var-value></var>
 </field>
....
....
  </form>
</formset>
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: http://struts.apache.org/userGuide/dev_validator.html. though there is example of validwhen but it does not contain any example like the condition in 7 .
Now if I write something like this:

<field property="term" depends="required,integer,intRange,validwhen">
<arg0 key="QuoteDetailsBean.term" />
<arg1 name="intRange" key="${var:min}" resource="false"/>
<arg2 name="intRange" key="${var:max}" resource="false"/>
<var><var-name>min</var-name><var-value>0</var-value></var>
<var><var-name>max</var-name><var-value>40</var-value></var>
<msg name="validwhen"
key="Age + Term cannot be more than 80" resource="false"/>
<var>
<var-name>test</var-name>
<var-value>(age + this < 80 )</var-value>
</var>

</field>
,
tomcat server gives exceptionr parsing the xml file when it starts up at the line <var-value>(age + this < 80 )</var-value> . If I change the line : <var-value>(age + this < 80 )</var-value> to <var-value>( this !=null)</var-value>, there is no exception thrown by tomcat when it starts. Ofcourse this!=null is not what I want.

so is the whole thing how I used validwhen wrong? Please let me know how should I do it. 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?
thanks
Tanveer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2007
Added on Aug 25 2004
7 comments
389 views