【发布时间】:2011-01-05 14:49:02
【问题描述】:
我们如何使用 hibernate 验证器 3.1.0.GA 执行跨字段验证
创建表用户(id, start_date, end_date, ...) 例如学生的大学毕业完成日期应大于毕业开始日期
我们如何强制执行此操作,以便验证消息可以在保存/更新操作时显示在 UI 中。 UI 使用 JSF、Richfaces 构建
【问题讨论】:
标签: seam richfaces hibernate-validator
我们如何使用 hibernate 验证器 3.1.0.GA 执行跨字段验证
创建表用户(id, start_date, end_date, ...) 例如学生的大学毕业完成日期应大于毕业开始日期
我们如何强制执行此操作,以便验证消息可以在保存/更新操作时显示在 UI 中。 UI 使用 JSF、Richfaces 构建
【问题讨论】:
标签: seam richfaces hibernate-validator
您可以通过创建custom validator. 来做到这一点。the documentation 中有更多信息。
【讨论】:
试试这个:
public class User {
private Date startDate = null;
private Date endDate = null;
@SuppressWarnings("unused")
@AssertTrue(message="college graduation finishing date for a student should be greater than the graduation start date")
private boolean dateValidation() {
return this.startDate < this.endDate;
}
}
【讨论】:
在Cross field validation with Hibernate Validator (JSR 303)查看我重复的问题(和答案)
简而言之,创建一个自定义类级别验证器,将类级别 ConstraintViolation 与正在验证的特定字段相关联。
【讨论】:
对于这种方法
@SuppressWarnings("unused")
@AssertTrue(message="college graduation finishing date for a student should be greater than the graduation start date") private boolean dateValidation() { return this.startDate < this.endDate; }
为了有效,它需要符合标准 bean 规范,这意味着 get/set/ 或 "is" 前缀。 isDateValid() 会起作用-
【讨论】: