0

I was wondering, am I overlooking something or does the hibernate validator offer no annotation to verify that 2 fields are equal (such as a password). I know I can write my own validators, but well this seems like standard functionality.

Mohsen
  • 438
  • 5
  • 14
jack
  • 1,902
  • 5
  • 32
  • 43
  • Implementation example: http://git.springsource.org/greenhouse/greenhouse/trees/master/src/main/java/com/springsource/greenhouse/validation – axtavt Dec 10 '10 at 13:31

4 Answers4

4

If you’re using Spring Framework then you can use Spring Expression Language (SpEL) for that. I’ve wrote small library that provides JSR-303 validator based on SpEL that makes cross-field validations very easy. Take a look at https://github.com/jirutka/validator-spring.

This will validate equality of password fields when at least one of them is not empty.

@SpELAssert(value = "password.equals(passwordVerify)",
            applyIf = "password || passwordVerify",
            message = "{validator.passwords_not_same}")
public class User {

    private String password;
    private String passwordVerify;
}
Jakub Jirutka
  • 10,269
  • 4
  • 42
  • 35
0

Just went for the custom validator route. The other 2 answers here aren't really related to the question. With a bit of googling I found a fieldmatch example.

jack
  • 1,902
  • 5
  • 32
  • 43
-1

Hibernate is a ORM Mapper.

It is used to persist data into a DB and extract it again. As such, having 2 fields with an identical value makes not much sense (From a persistance point of view). Thats something you should check in your Business logic.

And I am with Junesh... Dont persist your passwords in a retrievable format... Look up Hasing and Salting - Or even better, think about openID so you dont have to bother your clients with yet another stupid password...

Heiko Hatzfeld
  • 3,197
  • 18
  • 15
  • I used the hibernate tag because there wasn't a hibernate-validator tag and I can't create new ones yet. Ofcourse I'm not safing the confirm field. – jack Dec 10 '10 at 08:30
-2

I am hoping you are not saving the confirm password in the database as well. You do not have any out of the box validations for this, but instead you will have to use custom annotation which is pretty straight forward as well.

Jinesh Parekh
  • 2,131
  • 14
  • 18
  • I used the hibernate tag because there wasn't a hibernate-validator tag and I can't create new ones yet. Ofcourse I'm not safing the confirm field. – jack Dec 10 '10 at 08:29