-->
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.  [ 1 post ] 
Author Message
 Post subject: Need help for Custom Validator
PostPosted: Thu May 29, 2008 2:20 pm 
Beginner
Beginner

Joined: Tue Dec 27, 2005 1:13 pm
Posts: 25
Location: Kingston, ON, Canada
Hi, I created a custom hibernate validator like this. It seemed that the validator was not called. There was no error message at all.

I use JBoss Seam 2.0.2. All the codes are generated by the JBoss tools except this new validator.

Can anybody give any idea?

Thanks.


Declare:

Code:
@ValidatorClass(PasswordPolicyRestrictedValidator.class)
@Target({FIELD, METHOD})
@Retention(RUNTIME)
@Documented
public @interface PasswordPolicyRestricted {
  /** @return minimum number of characters from a-z (lower or upper case) */
  int minAlphas() default 0;

  /** @return minimum number of digits */
  int minDigits() default 0;

  /** @return error message key */
  String message() default "{validator.password_policy}";
}


Implement:

Code:
public class PasswordPolicyRestrictedValidator implements
    Validator<PasswordPolicyRestricted> {

  Pattern ALPHA = Pattern.compile("[a-zA-Z]");
  Pattern DIGIT = Pattern.compile("\\d");

  private PasswordPolicyRestricted policy;

  /** @see Validator#initialize(Annotation) */
  public void initialize(PasswordPolicyRestricted passwordPolicy) {
    this.policy = passwordPolicy;
  }

  public boolean isValid(Object o) {
    if (o == null) {
      return false;
    }
    if (o instanceof String) {
      return isValid((String) o);
    }
    return isValid(o.toString());
  }

  /**
   * @param password
   *            the password to check
   * @return whether password is valid according to its annotation
   */
  public boolean isValid(String password) {
    return check(password, policy.minAlphas(), ALPHA)
        && check(password, policy.minDigits(), DIGIT);
  }

  /**
   * @param sequence
   *            the sequence to check
   * @param min
   *            minimum number of {@code pattern}
   * @param pattern
   *            the pattern to check
   */
  private boolean check(String sequence, int min, Pattern pattern) {
    if (min > 0) {
      Matcher m = pattern.matcher(sequence);
      int count = 0;
      while (m.find()) {
        count++;
      }
      if (min > count) {
        return false;
      }
    }
    return true;
  }
}


Entity Bean:

Code:
@Entity
public class Person {
   
   @Id
   @GeneratedValue
   @Column(name="person_id")
   private Long id;
   
   @Column (name="password", length=20)
   @PasswordPolicyRestricted
   private String password;
.........
}


Page:

Code:
<s:decorate id="passwordDecoration" template="layout/edit.xhtml">
    <ui:define name="label">Password:</ui:define>
    <h:inputText id="password"
               size="20"
          maxlength="20"
          required="true"
              value="#{personHome.instance.password}">
        <a:support event="onblur" reRender="passwordDecoration" bypassUpdates="true"/>
    </h:inputText>
</s:decorate>


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

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.