You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Bailey, Shane C." <SH...@saic.com> on 2003/05/27 18:31:04 UTC

RE: Custom Validator not being called

Could it be that it is failing required or minlength
first and therefore isn't getting to your method?

-----Original Message-----
From: Brown, Melonie S. - Contractor [mailto:Melonie.Brown@forscom.army.mil]

Sent: Tuesday, May 27, 2003 12:32 PM
To: 'struts-user@jakarta.apache.org'
Subject: Custom Validator not being called

I have the standard validation working just fine.  I have attempted to
branch out to use a custom validator for making sure that passwords follow a
certain set of rules.  The custom validation method does not get called.   I
set the logging to debug, but the output only confirmed that my method isn't
being called.  Suggestions for how to debug further would be greatly
appreciated.

[log when standard validations fail]
DEBUG Date = 2003-05-27 12:22:54,543 [Thread-5]
org.apache.struts.action.RequestProcessor Line = 942 -  Validation failed,
returning to '/gotochangepassword.do'

[log when standard validations pass - should call my validator]
DEBUG Date = 2003-05-27 12:19:14,939 [Thread-4]
org.apache.struts.action.RequestProcessor Line = 915 -   No errors detected,
accepting input
 

Following are the relevant snippets of code:

Validator-rules.xml
--------------------------
   	
   	<validator name="complexpassword"
     classname="foo.SecurePasswordValidator"
     method="validateComplexPassword"
     methodparams="java.lang.Object,
                   org.apache.commons.validator.ValidatorAction,
                   org.apache.commons.validator.Field,
                   org.apache.struts.action.ActionErrors,
                   javax.servlet.http.HttpServletRequest" 
     msg="errors.changepasswordnotfollowrules">
     </validator>


Validation.xml
-----------------
<form name="changePasswordForm">
	   	   <field property="newPassword"
depends="required,minlength,complexpassword">
	   	   	  <arg0 key="changepassword.newpassword" />
			  <arg1 name="minlength" key="${var:minlength}"
resource="false"  />
			  <var>
			  	 <var-name>minlength</var-name>
				 <var-value>8</var-value>
			  </var>			  
	   	   </field>         		   
        	<field property="oldPassword" depends="required,minlength">
        		<arg0 key="changepassword.oldpassword" />
        		<arg1 name="minlength" key="${var:minlength}"
resource="false" />
			  <var>
			  	 <var-name>minlength</var-name>
				 <var-value>8</var-value>
			  </var>
        	</field>
        	 <field property="confirmnewPassword"
depends="required,minlength">
        		<arg0 key="changepassword.confirmpassword" />
        		<arg1 name="minlength" key="${var:minlength}"
resource="false"  />
			  <var>
			  	 <var-name>minlength</var-name>
				 <var-value>8</var-value>
			  </var>
        	</field>
        </form>



Validation method
--------------------

public static boolean validateComplexPassword(
        Object bean,
        ValidatorAction va,
        Field field,
        ActionErrors errors,
        HttpServletRequest request)
    {
        String value = null;
        if (isString(bean))
        {
            value = (String)bean;
        }
        else
        {
            value = ValidatorUtil.getValueAsString(bean,
field.getProperty());
        }
        boolean upperCase = false;
        boolean lowerCase = false;
        boolean digit = false;        
        for (int i = 0; i < value.length(); i++)
        {
            if (Character.isLowerCase(value.charAt(i)))
            {
                lowerCase = true;
            }
            if (Character.isDigit(value.charAt(i)))
            {
                digit = true;
            }
            if (Character.isUpperCase(value.charAt(i)))
            {
                upperCase = true;
            }            
        }
        // verify that the new password has the requisite combination of
elements (1, A, a)
        if (!(upperCase && lowerCase && digit))
        {
            errors.add(
                field.getKey(),
                Resources.getActionError(request, va, field));
            return false;
        }
        return true;
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org