apply custom validator in spring MVC controller in more than one class

前端 未结 2 1704
星月不相逢
星月不相逢 2021-02-11 06:00

I have one registration page which uses custom valiadtor

public class CustomValidator implements Validator {

    private Validator validator;

    public Custom         


        
2条回答
  •  孤独总比滥情好
    2021-02-11 06:36

    It appears you only want to validate 2 classes because you want to duplicate the 'confirm password' validation where you have 2 password fields that should match. Instead, why not simply pass an interface to the validator?

    public void validate(IPasswordContainer target, Errors errors) {
        if(!target.getConfirm_password().equals(target.getPassword())) {
            errors.rejectValue("confirm_password", "confirm_password.confirm");
        }
    }
    
    
    public boolean supports(Class clazz) {
        return IPasswordContainer.class.equals(clazz);
    }
    

提交回复
热议问题