• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Custom validation by validator-rules.xml is not working

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!