-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Custom Validator doesn't work
PostPosted: Sun Mar 07, 2010 8:23 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
I wrote my first custom validator for password confirmation. It doesn't work. I can't find the cause. Can someone point out what is missing from the following code:
Code:
public enum PasswordMode {

   SAME, DIFFERENT;
}

Code:
@Target( { TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = PasswordConstraintValidator.class)
@Documented
public @interface CheckPassword {
   
    String message() default "{com.abc.myapp.account.web.CheckPassword}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
   
    PasswordMode value();
}

Code:
@CheckPassword(value=PasswordMode.SAME)
public final class PasswordResetForm {
   
   @NotNull
   private String password;
   
   @NotNull
   private String passwordConfirmation;

        // .....

   /**
    * @return the password
    */
   public String getPassword() {
      return password;
   }

   /**
    * @param password the password to set
    */
   public void setPassword(String password) {
      this.password = password;
   }

   /**
    * @return the passwordConfirmation
    */
   public String getPasswordConfirmation() {
      return passwordConfirmation;
   }

   /**
    * @param passwordConfirmation the passwordConfirmation to set
    */
   public void setPasswordConfirmation(String passwordConfirmation) {
      this.passwordConfirmation = passwordConfirmation;
   }   
}

Code:
public final class PasswordConstraintValidator implements
      ConstraintValidator<CheckPassword, PasswordResetForm> {
   
   private PasswordMode passwordMode;

    public void initialize(CheckPassword constraintAnnotation) {
        this.passwordMode = constraintAnnotation.value();
    }

    public boolean isValid(PasswordResetForm passwordResetForm, ConstraintValidatorContext constraintContext) {
        if (passwordResetForm == null)
            return true;

        if (passwordMode == PasswordMode.SAME)
            return passwordResetForm.getPassword().equals(passwordResetForm.getPasswordConfirmation());
        else
            return !passwordResetForm.getPassword().equals(passwordResetForm.getPasswordConfirmation());
    }
}



Top
 Profile  
 
 Post subject: Re: Custom Validator doesn't work
PostPosted: Mon Mar 08, 2010 12:57 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Can you be more specific? Is there an exception? Have you turned on debug trace? What does say?
At the first glance the code look ok, you will have to provide more context.

--Hardy


Top
 Profile  
 
 Post subject: Re: Custom Validator doesn't work
PostPosted: Tue Apr 06, 2010 9:25 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
Thanks Hardy. It, in fact, works. My first test was not carefully carried out.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.