Writing Your Own Rules in Validator Framework

Writing Your Own Rules in Validator Framework

Previous Home Next

 

Create your own valiadtion rules is the powerful features in validator framework.To create your own validator rules and use it in a struts DynamicForm.Your web application may require a specific validation that is not provided by the struts validation framework. The struts validator framework provides a simple interface for creating your own custom validations that can easily be plugged into the validation framework.

 
You need to perform the following step :-

1.  Create a java class and  a new validation rule method in this class.
2.  Create an entry in the validation-rules.xml file.
3.  Applying new validation rule to form bean field inside validation.xml file.
4.  Add messages to the ApplicationResources.properties file.
5.  
Add a new <form> tag or modify an existing tag to indicate that this rule is to be applied when the form is validated.

Your own validator rules method get passed a bean ,validatorAction , Field .

Step 1.  Create a java class and  a new validation rule method in this class.

public class CustomValidatorRulesExample {  
public static boolean validatePhone(Object bean, ValidatorAction action, Field field, 
ActionMessages errors, Validator validator,	HttpServletRequest request) {	
-----------	}
} 

Step 2. Create an entry in the validation-rules.xml file.

<validator name="phone" classname="r4r.co.in.CustomValidatorRulesExample" method="validatePhone"  
 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.phone"/>

Step 3. Applying new validation rule to form bean field inside validation.xml file.

<field property="phone" depends="required,phone">    
<arg0 key="form.mobile" />
</field>

Step 4. Add messages to the ApplicationResources.properties file.

# Resources for parameter 'r4r.co.in.ApplicationResources'
# Project OwnValidatorRulesExample
# -- standard errors --errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an  byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
form.phone=Telephone no 
errors.phone='{0}' field is not a valid Phone no.
error.phoneNumber.required =Phone number is required.

Step 5.Add a new <form> tag or modify an existing tag to indicate that this rule is to be applied when the form is validated.


Previous Home Next