我有以下情况:
我有DoubleRangeValidator的字段,错误消息指定为field.addValidator(new DoubleRangeValidator(“Salary必须是数值。”,0d,1000000d));
在SAVE按钮下,我调用BeanFieldGroup.commit(),这会在此类字段中引发非数值的异常
显示错误我正在使用此处提到的ErrorUtils类在Vaadin 7中直接显示错误消息
..它只是为所有字段/组件调用component.getErrorMessage()并收集所有错误消息。 但是DoubleRange验证器的字段为getErrorMessage()返回null,因此这里没有可用的错误消息。 这同样适用于其他验证者。 所以我的问题是如何在commit()期间发生验证错误时显示验证器错误消息?
I have the followinf scenario:
I have field with DoubleRangeValidator with error message specified field.addValidator(new DoubleRangeValidator("Salary must be a numeric value.",0d,1000000d));
Under SAVE button I call BeanFieldGroup.commit() which raises exception on non numeric value in such field
To show errors I'm using ErrorUtils class mentioned here Display error messages directly in Vaadin 7
.. which simply calls component.getErrorMessage() for all fields / components and gather all error messages. But the field with DoubleRange validator returns null for getErrorMessage() so no error message is available here. The same apply for other validators. So my question is how can I show validator error messages when validation error occures during the commit() ?
最满意答案
我试图用你提到的ErrorUtils类重现你的错误,但getErrorMessage方法没有返回null 。 也许你创建BeanFieldGroup和/或Field 。 这是我的代码:
@Override protected void init(VaadinRequest request) { BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<>(MyBean.class); bfg.setItemDataSource(new MyBean()); Field<?> salaryField = bfg.buildAndBind("salary"); salaryField.addValidator(new DoubleRangeValidator("Salary must be a numeric value.", 0d, 1000000d)); Button commit = new Button("Save", e -> { try { bfg.commit(); } catch (CommitException ce) { ErrorUtils.showComponentErrors(bfg.getFields()); } }); VerticalLayout hLayout = new VerticalLayout(salaryField, commit); setContent(hLayout); }public class MyBean implements Serializable { private static final long serialVersionUID = 1L; private double salary; public MyBean() {} public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }I've tried to reproduce your error with the ErrorUtils class you mentioned but the getErrorMessage method did not returned null. Maybe something is wrong with how you create your BeanFieldGroup and/or Field. Here's my code:
@Override protected void init(VaadinRequest request) { BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<>(MyBean.class); bfg.setItemDataSource(new MyBean()); Field<?> salaryField = bfg.buildAndBind("salary"); salaryField.addValidator(new DoubleRangeValidator("Salary must be a numeric value.", 0d, 1000000d)); Button commit = new Button("Save", e -> { try { bfg.commit(); } catch (CommitException ce) { ErrorUtils.showComponentErrors(bfg.getFields()); } }); VerticalLayout hLayout = new VerticalLayout(salaryField, commit); setContent(hLayout); }public class MyBean implements Serializable { private static final long serialVersionUID = 1L; private double salary; public MyBean() {} public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
更多推荐
发布评论