Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Custom validation by validator-rules.xml is not working

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic