验证错误发生时如何向用户显示验证器错误消息?(How to show validator error messages to user when validation error occures?)

编程入门 行业动态 更新时间:2024-10-27 02:22:23
验证错误发生时如何向用户显示验证器错误消息?(How to show validator error messages to user when validation error occures?)

我有以下情况:

我有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;
    }
}

                    
                     
          

更多推荐

本文发布于:2023-08-04 22:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   发生   消息   用户   show

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!