使用 BeanFieldGroup 进行 Vaadin 表单验证
Vaadin Form Validation with BeanFieldGroup
在 Vaadin 的书中我读到了以下内容:
Validating a bean is done with a BeanValidator, which you initialize with the name of the bean property it should validate and add it the the editor field. The validation is done immediately after focus leaves the field. Bean validators are automatically created when using a BeanFieldGroup.
这是我的 BeanFieldGroup:
BeanFieldGroup<OrderSearchCriteria> binder = new BeanFieldGroup<>(OrderSearchCriteria.class);
binder.setItemDataSource(searchCriteria);
这就是我在我的 bean 上添加验证的方式:
@Min(0)
private BigDecimal minAmount;
@Min(0)
private BigDecimal maxAmount;
@Pattern(regexp = "([a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]{2,})?")
private String email;
验证没有像我想象的那样自动添加。那我错过了什么?
您需要下载一个 bean 验证器的实现,然后一切都会开箱即用。
如果您正在使用 Maven 并想使用 hibernate-validator,您只需在 pom.xml 文件中添加一个依赖项,如下所示:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>
参考文献:
- Hibernate 验证器:http://hibernate.org/validator/
- Maven 中可用的版本:http://mvnrepository.com/artifact/org.hibernate/hibernate-validator
在 Vaadin 的书中我读到了以下内容:
Validating a bean is done with a BeanValidator, which you initialize with the name of the bean property it should validate and add it the the editor field. The validation is done immediately after focus leaves the field. Bean validators are automatically created when using a BeanFieldGroup.
这是我的 BeanFieldGroup:
BeanFieldGroup<OrderSearchCriteria> binder = new BeanFieldGroup<>(OrderSearchCriteria.class);
binder.setItemDataSource(searchCriteria);
这就是我在我的 bean 上添加验证的方式:
@Min(0)
private BigDecimal minAmount;
@Min(0)
private BigDecimal maxAmount;
@Pattern(regexp = "([a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]{2,})?")
private String email;
验证没有像我想象的那样自动添加。那我错过了什么?
您需要下载一个 bean 验证器的实现,然后一切都会开箱即用。
如果您正在使用 Maven 并想使用 hibernate-validator,您只需在 pom.xml 文件中添加一个依赖项,如下所示:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>
参考文献:
- Hibernate 验证器:http://hibernate.org/validator/
- Maven 中可用的版本:http://mvnrepository.com/artifact/org.hibernate/hibernate-validator