方法上的Beanvalidation
public class Register { @NotNull private String password; @NotNull private String passwordRepeat; @AssertTrue private boolean comparePasswords() { return password.equals(passwordRepeat); } private Set<ConstraintViolation> violations; public void doRegister(AjaxBehaviorEvent event) { Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); violations = validator.validate(this); if(violations.isEmpty()) { // This occurs } } }
如果我的密码都不为空,我的validation将通过,但它们是不同的。 似乎最后的约束似乎不会被考虑在内,尽管我不知道为什么。 有没有人有建议?
不,我不是在寻找@Matches或simular自定义validation器的任何实现。 我只想解决这个问题。
提前致谢。
更新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> violations; private FacesContext context; private Validator validator; @PostConstruct public void init() { context = FacesContext.getCurrentInstance(); validator = Validation.buildDefaultValidatorFactory().getValidator(); example1 = "abc"; example2 = "def"; runValidation(false, 1); example1 = "abc"; example2 = "abc"; runValidation(true, 2); example1 = "abc"; example2 = null; runValidation(false, 3); } private void runValidation(boolean assertion, int testNr) { FacesMessage message; violations = validator.validate(this); if(violations.isEmpty() == assertion) { message = new FacesMessage("Passed test nr. " + testNr); } else { message = new FacesMessage("Failed test nr. " + testNr); } context.addMessage(FacesMessage.FACES_MESSAGES, message); }
的index.xhtml
TODO supply a title #{bean}
结果
beanvalidation.Bean@31c5c3da Failed test nr. 1 Passed test nr. 2 Passed test nr. 3
examplesMatch()不是有效的Java Beans布尔属性getter。 它需要以get或is开头。
- 使用JSR-303和Spring的Validator的组合为spring引导端点实现自定义validation逻辑
- 在@Constraint中清空validatedBy
- beanvalidation默认参数的顺序?
- 如何使用@DecimalMin和@DecimalMaxvalidationList ?
- Hibernate – 激活Bean Validation集成时出错
- Spring MVC和JSR-303 hibernate条件validation
- 在JSF中使用beanvalidation进行本地化
- JSF:如何validation字段并通过beanvalidation返回错误消息?
- Hibernatevalidation失败时出现意外的UnsupportedOperationException