5

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ben Lamm
  • 603
  • 8
  • 18

2 Answers2

3

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.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • That worked! Thank you. I would still like to understand how to make it work as an expression validator. The two links above seem to be describing the same functionality just in different words. – Ben Lamm Mar 01 '13 at 17:54
  • @BLam85: Updated my answer a little bit. You can use expression validator but to view errors in JSP you need to use `` tag. – Aleksandr M Mar 02 '13 at 11:03
2

try

%{password == repassword}

The validator checks to boolean OGNL expression that both are equal.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    That did not work... as expected. The validator failed whether or not the passwords matched. (i.e. The action returned input) However the message was never displayed and i see the following warning in the console `Mar 1, 2013 12:59:28 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn WARNING: Got result of null when trying to get Boolean.` – Ben Lamm Mar 01 '13 at 18:03
  • This OGNL expression evaluates to true if the values match. – Roman C Mar 01 '13 at 18:15