Struts Form Validation
Struts Form Validation
Go Through
1.
Form Validation
2.
3.
4.
Validator-rules.xml
5.
6.
Validation.xml
7.
ValidatorForm class
8.
9.
Example
Volvo IT
Struts Form Validation
2
1.Form Validation
Volvo IT
Struts Form Validation
3
2. validate() method:
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return super.validate(mapping, request);
}
Validate() is to verify that the input submitted by the user
are correct and valid.
It returns a ActionErrors object, which consists of number of
message keys that is used to look up the text from the
appropriate message resource file.
Volvo IT
Struts Form Validation
4
Volvo IT
Struts Form Validation
5
4.validator-rules.xml
It is called as global rules file where all the rules are actually
defined.
Ex:- validation for Email, Number, phoneNo etc.
Each <validator> element defines one validation rule.
Struts provides FieldChecks class as a validator that uses
the GenericValidator and have methods like
validateRequired(), validateDate(), validateCreditCard().
Sample of a validator-rules.xml is:
Volvo IT
Struts Form Validation
6
<form-validation>
<global>
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
<validator name=>
</validator>
<!- More validators defined here -->
</global>
</form-validation>
validator-rules.xml
Volvo IT
Struts Form Validation
7
Volvo IT
Struts Form Validation
8
6.validation.xml
Application Specific(one per application).
It associates rules from global rule file with individual fields of
ActionForm.
The xml consists of a <formset> block with multiple <form>
blocks, one for each form.
Each form element is given its own name. This should
correspond to the form bean name.
Each form element is composed of a number of field elements.
The field elements designate which validator(s) to use with the
depends attribute.
Volvo IT
Struts Form Validation
9
<form-validation>
<formset>
<form name="registerForm">
<field property="firstName"
depends="required,minlength">
<arg0 key="label.firstName"/>
<arg1 name ="minlength" key="${var:minlength}"
resource="false"/>
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
</field>
<field property="lastName" depends="required">
<arg0 key="label.lastName"/>
</field>
</form>
</formset>
</form-validation>
validation.xml
Volvo IT
7. ValidatorForm class:
How validation failure became Action Error?
Here we have to extend our form from ValidatorForm.
ValidatorForm is a subclass of ActionForm and implements
the validate() method.
It executes the rules using the two xml files and generates
ActionErrors using the Message Resources defined in the
struts-config.xml.
Volvo IT
Struts Form Validation
12
Volvo IT
Struts Form Validation
13
2.
Add the corresponding <form> element with <field> subelement for every form field that needs validation.
3.
4.
5.
Volvo IT
Struts Form Validation
14
9.Example:
http://localhost:8080/StrutsValidateForm/
Volvo IT
Struts Form Validation
15
Any Questions ?
Volvo IT
Struts Form Validation
16