我在Window中有字段,有些带有验证器,所有字段都绑定到属性。 验证按预期工作。
但是 -
当任何字段无效时,我不想继续。确定是否有任何验证错误的最佳方法是什么?
答案 0 :(得分:1)
在Vaadin中有几种处理验证的方法,所有这些都由Vaadin支持(不需要自定义布尔值afterValidationFlag)。 一种可能的方式(由我提供)如下所示:
public class CustomWindow extends Window {
DateField customBeanFirstPropertyName = new DateField("Caption1");
ComboBox customBeanSecondPropertyName = new ComboBox("Caption2");
TextArea customBeanThirdPropertyName = new TextArea("Caption3");
BeanFieldGroup<CustomBean> binder = new BeanFieldGroup<>(CustomBean.class);
public CustomWindow(CustomBean customBean) {
buildLayout();
binder.buildAndBindMemberFields(this);
binder.setItemDataSource(new BeanItem<>(customBean));
//add validators
customBeanFirstPropertyName.addValidator((Validator) value -> {
if (value == null) throw new Validator.InvalidValueException("nonnull required");
});
customBeanThirdPropertyName.addValidator(
new RegexpValidator(".{3,20}", "length between 3-20 required")
);
/*
or have basic validators on @Entity level with e.g. javax.validation.constraints.Size
example:
@Size(min = 3, max = 20)
@Column(name = "customBeanThirdPropertyName", unique = true)
private String customBeanThirdPropertyName;
*/
}
void commit(Button.ClickEvent event) { //method called by "save" button
try {
binder.commit(); //internally calls valid() method on each field, which could throw exception
CustomBean customBeanAfterValidation = binder.getItemDataSource().getBean(); //custom actions with validated bean from binder
this.close();
} catch (FieldGroup.CommitException e) {
Map<Field<?>, Validator.InvalidValueException> invalidFields = e.getInvalidFields(); //do sth with invalid fields
}
}
}
答案 1 :(得分:1)
如果您使用FieldGroup实例将字段与属性绑定(这是推荐的方式),您可以写:
{"access_token":"c.KDYfQh2vrkS0w4k4BtGcJPysmzAzb3uXNz2myCMcEko5dUnrs5022kPd6cJnn5sg97hzXbm9YDzkDELXiLW58Frj6b2GyawbWLQMmm2x0eqmqa0j9VMpVQz2UXZ0mC2nDp7EmgVqsqgAXXA6","expires_in":315360000}
检查字段组管理的字段的所有字段验证。
答案 2 :(得分:0)
保持旗帜。在继续之前,请检查是否已设置标志。在验证代码中,如果验证失败,请设置标志。