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

前端 未结 2 2016
有刺的猬
有刺的猬 2021-02-11 05:51

I have one registration page which uses custom valiadtor

public class CustomValidator implements Validator {

    private Validator validator;

    public Custom         


        
2条回答
  •  时光说笑
    2021-02-11 06:56

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

提交回复
热议问题