Validator Framework



Validator framework is an optional sub framework of struts that provides predefines validation logic for common validation problem. Validation logic is provided in the form of static methods called validation rules. When validator framework is used for form data validation your ActionForm must be extends ValidatorForm class.
Framework has following components:
1.    Set of rules defined in a xml file known as validator-rules.xml
2.    An xml file named validation.xml used by the application developer to apply validation rules on a form.

Validator-rules.xml
<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="minlength"
                   classname="org.apache.struts.validator.FieldChecks"
                   method="validateMinLength"
                   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.minlength"
                   jsFunction="org.apache.commons.validator.javascript.validateMinLength"/>
</global>
</form-validation>
Validation.xml
<form-validation>
<formset>
          <form name="MyActionForm">
            <field    property="name"   depends="required,minlength">
                    <arg0 key="MyActionForm.name"/>
                    <arg1 key="10" resource="false" />
                   <var>
                       <var-name>minlength</var-name>
                       <var-value>10</var-value>
                   </var>
            </field>
            <field   property="id"  depends="required,mask">
                    <arg key="MyActionForm.id"/>
                    <msg key="errors.email.msg" name="mask"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>^[0-9a-zA-Z]*$</var-value>
                    </var>
            </field>
        </form>
    </formset>
</form-validation>

Exp:-
<form-validation> is the root element
<formset> sub element of <form-validation> contains set of form
<form name=”MyForm”> sub element of <formset> contains form element
<field  property="id"  depends="required,mask"> sub element of <form> contains field information for    validation

property define property name and depends define validation rule applied on that property.
<arg key="error.msg" resource="false"> sub element of <field> specify the value of error message place holder if resource is false that means value is directly replaced by key or true means value is get form property file.
<msg key="errors.email.msg" name="mask"> sub element of <field> specify message for a validation rule.
<var> sub element of <field> is used for provide input value for variable.

Mask Validation Rule is used to validate value of a field using regular expression.

<global>       
        <constant>
            <constant-name>postal</constant-name>
            <constant-value>^[0-9]{2}\*?[0-9]+$</constant-value>
        </constant>  
  </global>
<formset>
   <field
                property="id"
                depends="required,mask">
                    <arg key="MyActionForm.id"/>
                    <msg key="errors.email.msg" name="mask"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>${postal}</var-value>
                    </var>
            </field>
        </form>
</fromset>

No comments:

Post a Comment