Cross field validation with Hibernate Validator (JSR 303)

前端 未结 15 1773
渐次进展
渐次进展 2020-11-22 02:37

Is there an implementation of (or third-party implementation for) cross field validation in Hibernate Validator 4.x? If not, what is the cleanest way to implement a cross fi

15条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 02:57

    I have tried Alberthoven's example (hibernate-validator 4.0.2.GA) and i get an ValidationException: „Annotated methods must follow the JavaBeans naming convention. match() does not.“ too. After I renamed the method from „match“ to "isValid" it works.

    public class Password {
    
        private String password;
    
        private String retypedPassword;
    
        public Password(String password, String retypedPassword) {
            super();
            this.password = password;
            this.retypedPassword = retypedPassword;
        }
    
        @AssertTrue(message="password should match retyped password")
        private boolean isValid(){
            if (password == null) {
                return retypedPassword == null;
            } else {
                return password.equals(retypedPassword);
            }
        }
    
        public String getPassword() {
            return password;
        }
    
        public String getRetypedPassword() {
            return retypedPassword;
        }
    
    }
    

提交回复
热议问题