javastruts2validationognl

Comparing two string values in validation.xml


I am new to Struts 2 and OGNL and am making a simple web application with a registration page. There are two fields, password and repassword (to re-enter the password) and using the validation framework. I would like to validate that the two passwords match (I know that I can do it easily with JavaScript). Here is what I've got so far. All of the field-validators are working fine. This is my first non-field validator, and I just can't get it to work.

<validator type="expression">
    <param name="expression">${password}!=${repassword}</param>
    <message>Passwords must match.</message>
</validator> 

I tried both with

${password}!=${repassword}

and without

password!=repassword

the OGNL tags.


Solution

  • The expression validator is a Non-Field Level validator. Use fieldexpression validator which is a Field Level validator and validates using OGNL expression. And it must be equals (==) check.

    <field name="password">
      <field-validator type="fieldexpression">
        <param name="expression"><![CDATA[password == repassword]]></param>
        <message>Passwords must match.</message>
      </field-validator>
    </field>
    

    The expression validator adds action errors. The fieldexpression validator adds field errors.