SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Struts Validator problems with null message values

    I often have problems with my message substitution values when multiple validation rules apply to one field. In the example below if I type a password which is too short I get the message:

    Password must be greater than 5 characters long.

    This is correct. However if I type a password which is too long I get:

    Password must be less than null characters long.

    Obviously this is wrong.

    I have the following form bean in Struts Config.xml:
    ...
    <form-bean name="signup" type="org.apache.struts.validator.DynaValidatorForm">
    <form-property name="name" type="java.lang.String"/>
    <form-property name="email" type="java.lang.String"/>
    <form-property name="password" type="java.lang.String"/>
    <form-property name="confirmPassword" type="java.lang.String"/>
    </form-bean>
    ...

    and the following in Valiation.xml
    ...
    <form name="signup">
    <field property="name" depends="required,mask,maxlength">
    <arg0 key="validation.name"/>
    <arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
    <var>
    <var-name>mask</var-name>
    <var-value>^[0-9a-zA-Z]*$</var-value>
    </var>
    <var>
    <var-name>maxlength</var-name>
    <var-value>20</var-value>
    </var>
    </field>
    <field property="email" depends="required,email">
    <arg0 key="validation.email"/>
    </field>
    <field property="password" depends="required,mask,minlength,maxlength">
    <arg0 key="validation.password"/>
    <var>
    <var-name>mask</var-name>
    <var-value>^[0-9a-zA-Z]*$</var-value>
    </var>
    <arg1 name="minlength" key="${var:minlength}" resource="false"/>
    <var>
    <var-name>minlength</var-name>
    <var-value>5</var-value>
    </var>
    <arg2 name="maxlength" key="${var:maxlength}" resource="false"/>
    <var>
    <var-name>maxlength</var-name>
    <var-value>20</var-value>
    </var>
    </field>
    <field property="confirmPassword"
    depends="required,mask,minlength,maxlength,twofields">
    <arg0 key="validation.confirmPassword"/>
    <var>
    <var-name>mask</var-name>
    <var-value>^[0-9a-zA-Z]*$</var-value>
    </var>
    <arg1 name="minlength" key="${var:minlength}" resource="false"/>
    <var>
    <var-name>minlength</var-name>
    <var-value>5</var-value>
    </var>
    <arg2 name="maxlength" key="${var:maxlength}" resource="false"/>
    <var>
    <var-name>maxlength</var-name>
    <var-value>20</var-value>
    </var>
    <arg3 key="validation.confirmPassword" resource="false"/>
    <var>
    <var-name>secondProperty</var-name>
    <var-value>password</var-value>
    </var>
    </field>
    </form>
    ...

    Any Ideas?

    Thanks.

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    First off, the arg0...arg5 tags are deprecated, use <arg position='n'... instead.
    Not only are they deprecated, but the intent is now clearer. arg3, or arg position='3', is not the third arg for that field, but the arg for the third position in the message of the particluar rule being addressed.

    The error message for maxlength is:
    errors.maxlength={0} can not be greater than {1} characters.

    So you must provide an arg position='1' value for maxlength for the replacement to work properly. (as opposed to the arg position='2' you currently have)

    You can do this by providing the name of the validator rule in the arg tag, as below
    <arg position="1" name="maxlength" key="32" resource="false"/>

    Likewise, you can provide the arg for minlength position='1' in the same way:
    <arg position="1" name="minlength" key="5" resource="false"/>

    If you omit the name attribute, then it will be a general arg and used for all the rules in that field validation, such as:
    <arg position="0" key="validation.password"/>

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excelent, many thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •