Announcement Announcement Module
Collapse
No announcement yet.
validation annotations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 ( http://jee-bpel-soa.blogspot.com/200...framework.html ) 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?

    Comment


    • #3
      Hi,

      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
      Code:
      @RepeatedPassword
      public class User { ... }
      2) Create you custom constraint
      Code:
      @Documented
      @Target({ElementType.TYPE})
      @Retention( RetentionPolicy.RUNTIME )
      @Constraint(validatedBy=RepeatedPasswordValidator.class)
      public @interface RepeatedPassword {
      	String message() default "cz.alvap.validation.RepeatedPassword.message;
              Class<?>[] groups() default {};
              Class<? extends Payload>[] payload() default {};
      }
      3) Defined the validator
      Code:
      public class RepeatedPasswordValidator implements ConstraintValidator<RepeatedPassword,User> {
         
          @Override
          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;
            
          }
      
          @Override
          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

      Comment


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

        Comment

        Working...
        X