aspose file tools*
The moose likes Struts and the fly likes Custom validation by validator-rules.xml is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Custom validation by validator-rules.xml is not working" Watch "Custom validation by validator-rules.xml is not working" New topic
Author

Custom validation by validator-rules.xml is not working

abhi jitnag
Greenhorn

Joined: Oct 17, 2010
Posts: 13

Posts: 9

[Post New]posted Today 09:24:18
0
Quote Edit
I have written a java class for custom validation and update validation.xml, validator-rules.xml and struts-config.xml as below.

struts-config.xml

<form-bean name="emp_details" type="bean.EmpDetails"/>

<action-mappings>
<action path="/EmpSaveAaction"
type="action.hh"
name="emp_details"
scope="request"
validate="true"
input="/Rgistration.jsp"/>

<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>


validation.xml

<form name="emp_details">
<field property="uname" depends="required">
<arg position="0" key="User Name" resource="false"/>
</field>
<field property="password" depends="required,mask,minlength,maxlength,password">
<arg position="0" key="password" resource="false"/>
<arg position="1" name="minlength" key="${var:minlength}" resource="false"/>
<arg position="1" name="maxlength" key="${var:maxlength}" resource="false"/>
<arg position="1" name="mask" key="${var:mask}" resource="false"/>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z]</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>10</var-value>
</var>
</field>
</form>

validator-rules.xml

<validator name="password"
classname="customvalidation.AbhiValidation"
method="validatePasswordchk"
methodParams="java.lang.Object,
org.apache.commons.validator.Field,
javax.servlet.http.HttpServletRequest,
org.apache.commons.validator.Validator,
org.apache.commons.validator.ValidatorAction,
org.apache.struts.action.ActionMessages"
msg="errors.required"/>



And the java class for custom validation


import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.Resources;

class AbhiValidation{
public static boolean validatePasswordchk(Object bean,ValidatorAction action,
Field field,ActionMessages errors,Validator validator,HttpServletRequest request){
String pass=ValidatorUtil.getValueAsString(bean,field.getProperty());
if(pass!=null){
if(pass.contains("-")){
errors.add(field.getKey(),Resources.getActionMessage(validator,request, action, field));
return false;
}
}
return true;
}



But during run control is not coming in java class.
The following exception has given

customvalidation.AbhiValidation.validatePasswordchk(java.lang.Object, org.apache.commons.validator.Field, javax.servlet.http.HttpServletRequest, org.apache.commons.validator.Validator, org.apache.commons.validator.ValidatorAction, org.apache.struts.action.ActionMessages)
org.apache.commons.validator.ValidatorException: customvalidation.AbhiValidation.validatePasswordchk(java.lang.Object, org.apache.commons.validator.Field, javax.servlet.http.HttpServletRequest, org.apache.commons.validator.Validator, org.apache.commons.validator.ValidatorAction, org.apache.struts.action.ActionMessages)
at org.apache.commons.validator.ValidatorAction.loadValidationMethod(ValidatorAction.java:627)

Could anyone please help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom validation by validator-rules.xml is not working