Announcement Announcement Module
No announcement yet.
validation annotations Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • validation annotations

    So I just upgraded to RC2 and am testing with the new validations. I am trying to do a confirmation password validation and am having trouble finding the appropriate annotation. I need to be able to annotate the field as something like:
    @EqualTo(field="originalPassword",message="Confirm password does not match validation password.")

    I found the EqualToField annotation here ( ) but spring does not appear to support it or it is in a different jar besides the validation jar. Can anyone point me to an example of this????

    What is the right annotation???

  • #2
    no word on this?

    Anyone know how to reference another field's value in the validation annotation?


    • #3

      if you want to validate more fields, use bean level validation constraint - this way you get access to bean object and can for example compare two fields.

      This is code I use for password validation, works well, may help:

      1) On bean level define bean validation constraint
      public class User { ... }
      2) Create you custom constraint
      @Retention( RetentionPolicy.RUNTIME )
      public @interface RepeatedPassword {
      	String message() default "cz.alvap.validation.RepeatedPassword.message;
              Class<?>[] groups() default {};
              Class<? extends Payload>[] payload() default {};
      3) Defined the validator
      public class RepeatedPasswordValidator implements ConstraintValidator<RepeatedPassword,User> {
          public boolean isValid(User user, ConstraintValidatorContext context) {
              if (user.getNonCodedPassword() == null || user.getPasswordRepeated() == null) return true;
              if (user.getNonCodedPassword().equals(user.getPasswordRepeated()))
                  return true;
              return false;
          public void initialize(RepeatedPassword constraintAnnotation) {
      This is the standard way how to define custom constraints - it is all well described in Hibernate Validator 4.0 reference.

      Best regards, Pavla


      • #4
        Thanks very much Pavla, that was helpful. But I think you're missing a closing quote in this line:
         String message() default &quot;cz.alvap.validation.RepeatedPassword.message;