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>