Cross field bean validation - why you no work

筅森魡賤 提交于 2019-12-01 14:41:47

Class-level constraints are not triggered automatically by JSF during validation phase. You can use only field-level constraints (moreover not all fields are valuated by JSF but only those that are in your facelet).

If you want to use bean validation you can perform validation manually:

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Test>> violations = validator.validate( this, Default.class );

You can do it in registration method of your bean or after update model phase (but I've never tried it).

Anyway you can use JSF validation instead of bean validation or check the passwords directly in the registration method:

public String registration() {
    ...

    if ( !password.equals(password2) ) {
        FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( "Passwords do not match" ) );
        return null;
    }

    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!