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:
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 .
I tried by doing this:
<field property="term" depends="required,integer,intRange,
validwhen">
<msg
name="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><var-name>min</var-name><var-value>0</var-value></var>
<var><var-name>max</var-name><var-value>40</var-value></var>
<var>
<var-name>test</var-name>
<var-value> ((*this* + age) < 80)</var-value>
</var>
</field>
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)
java.lang.Class.privateGetDeclaredMethods(Class.java:1655)
java.lang.Class.getMethod0(Class.java:1901)
java.lang.Class.getMethod(Class.java:984)
org.apache.commons.validator.ValidatorAction.loadValidationMethod(ValidatorAction.java:623)
org.apache.commons.validator.ValidatorAction.executeValidationMethod(ValidatorAction.java:557)
org.apache.commons.validator.Field.validateForRule(Field.java:811)
org.apache.commons.validator.Field.validate(Field.java:890)
org.apache.commons.validator.Form.validate(Form.java:174)
org.apache.commons.validator.Validator.validate(Validator.java:367)
org.apache.struts.validator.DynaValidatorForm.validate(DynaValidatorForm.java:112)
org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:921)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:206)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1158)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
[/CODE]
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?
thanks
Tanveer
[ August 25, 2004: Message edited by: Tanveer Rameez ]