Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts 2 OGNL - Comparing two string values in validation.xml

I am new to Struts2 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 cant 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.

like image 511
Blam Avatar asked Feb 28 '13 21:02

Blam


1 Answers

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.

like image 175
Aleksandr M Avatar answered Sep 18 '22 23:09

Aleksandr M