public class Register {
@NotNull private String password;
@NotNull private String passwordRepeat;
@AssertTrue private boolean comparePasswords() {
return password.equals(passwordRepeat);
}
private Set<ConstraintViolation<Register>> violations;
public void doRegister(AjaxBehaviorEvent event) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
violations = validator.validate(this);
if(violations.isEmpty()) {
// This occurs
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我的密码都不为空,我的验证将通过,但它们是不同的.似乎最后的约束似乎不会被考虑在内,尽管我不知道为什么.有没有人有建议?
不,我不是在寻找@Matches或simular自定义验证器的任何实现.我只想解决这个问题.
提前致谢.
更新1
我已经对此进行了一些测试,希望结果将提供所需的信息.
Bean.java
@Named
@RequestScoped
public class Bean {
@NotNull private String example1;
@NotNull private String example2;
@AssertTrue private boolean examplesMatch() { return example1.equals(example2); }
private Set<ConstraintViolation<Bean>> violations;
private FacesContext context;
private Validator validator; …Run Code Online (Sandbox Code Playgroud)