Cross field bean validation - why you no work

后端 未结 1 601
生来不讨喜
生来不讨喜 2021-01-16 16:23

I\'ve got a little problem here with my application. I\'d like to check if fields password and confirm password match together, so I tried to do it like in the first answer

1条回答
  •  梦毁少年i
    2021-01-16 16:50

    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> 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;
        }
    
        ...
    }
    

    0 讨论(0)
提交回复
热议问题