Mask Rule Rules in Validator Framework

Mask Rule Rules in Validator Framework

Previous Home Next

 

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.

 When you use the mask rule, you are implementing the rule in regular expression syntax instead of java.Since you are writing your own rule ,starting with a blank state if you will, the default message often does not make sense.
The following step for using the mask rule and defining your own custom message are:-

1. Specify that the user name field correspond to the mask rule.

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>  
<body>   
<html:form action="/userAction" > 
<table>  
<tr><td>  User Name : </td>  
<td> 
<html:text property="userName" />  
</td> <td><html:errors property="userName" /> 
</td></tr>     
<tr><td>Registration Number : </td> 
<td> 
<html:text property="registrationno" />   
</td>  
<td> <html:errors property="registrationno" />  
</td> </tr> 
<tr>      
<td colspan="4" align="center"> 
<html:submit /> 
</td>
</tr>  
</table> 
</html:form>
</body> 
</html>

2. Specify an error message key for mask rule.

3. Add the error message for the mask rule to the resource bundle.

# Resources for parameter 'com.yourcompany.struts.ApplicationResources' 
# Project maskrulesvalidation
errors.integer={0} must be an integer. 
errors.general=The process did not complete.Details should follow. 
errors.token=Request could not be completed.Operation is not in sequence. 
userForm.username.mask = {0} should contain only alphabets. 
userForm.registrationno.mask = {0} should contain only numbers. 
userForm.username = User Name 
userForm.registrationno =Registration No

4. Configure the value of the mask variable to a pattern that implements yo business rules.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC      
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"        
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd"> 
<form-validation> 
<formset>    
<form name="userForm"> 
<field property="userName" depends="required,mask">  
<msg name="mask" key="userForm.username.mask" /> 
<arg key="userForm.username"/>
<var>  
<var-name>mask</var-name> 
<var-value>^[a-zA-Z]*$ </var-value>   
</var>     
</field>   
<field property="registrationno" depends="required,mask">  
<msg name="mask" key="userForm.registrationno.mask" />
<arg key="userForm.registrationno"/> 
<var>       
<var-name>mask</var-name>  
<var-value>^[0-9]*$</var-value>  
</var>  
</field>
</form> 
</formset>
</form-validation> 

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="userForm" 
type="org.apache.struts.validator.DynaValidatorForm" >
<form-property name="userName" type="java.lang.String" />
<form-property name="registrationno" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action input="/user.jsp" name="userForm" 
path="/userAction" scope="request" type="r4r.co.in.UserAction">
<forward name="success" path="/thank.jsp" />
</action>        
</action-mappings>
<message-resources parameter="r4r.co.in.ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" 
value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>


Previous Home Next