Common Validator Rules in Validator Framework

Common Validator Rules in Validator Framework

Previous Home Next

 

Validation framework comes with set of useful routines to handle form validation automatically and it can handle both server side as well as client side form validation.Thee validator-rules.xml file serves as the deployment descriptor for the validation rules components.

 There are two XML configuration files in Validator Framework:-

1.validator-rules.xml:-This files specifies the validation rules available. As the Validator comes with several default rules.This file contains the default Struts pluggable validator definitions.2.validation.xml :-This files enables you to set up the mapping from the Action form's property to the rules and any error message for the rules.

There are some standard rules in validator framework :-

1. required -
mandatory field validation. It must be present for the form to be valid and takes no variables.

<field property="name" depends="required">
<arg0 key="user.name"/>
</field>


2.minlength :- 
validate input data isn't less than a specified minimum length. Requires a minlength variable.

<field property="name" depends="required,minlength">
<arg0 key="user.name"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<var><var-name>minlength</var-name><var-value>2</var-value></var>
</field>

3.maxlength :-
validate input data doesn't exceed a specified maximum length. Requires a maxlength variable

<field property="name" depends="required,maxlength">
<arg0 key="user.name"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<var><var-name>maxlength</var-name><var-value>30</var-value></var>
</field>

4.mask :-
 The Mask Rule  Rules is flexible and eliminates the need for writing a lot of custom validation  rules.The mask rule uses Perl 5-style regular expression.

<validator name="mask"
classname="org.apache.struts.validator.FieldChecks"
method="validateMask"
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"
depends="" msg="errors.invalid"/>

5. byte ,short,integer,long,float,double :- 
validates that a field can be converted to a any types.

<field property="mobileno" depends="interger">
<arg0 key="employee.mobileno"/> 
</field>

6.date :-
validates that a field can be converted to a Date. This validator uses java.text.SimpleDateFormat to parse the date and optionally either a datePattern or datePatternStrict variable can be used. 

7.Email :- 
The field must be a possible e-mail address.

<field property="email" depends="required,email">	 
<msg name="required" key="err.user.email.required" />
<msg name="email" key="err.user.email.invalid" />
</field>

Previous Home Next