Vaadin 如何以编程方式在网格编辑器字段中显示错误消息?(瓦丁13)

Vaadin 如何以编程方式在网格编辑器字段中显示错误消息?(瓦丁13),vaadin,vaadin-grid,vaadin-flow,Vaadin,Vaadin Grid,Vaadin Flow,我有一个用于编辑bean的网格,它为bean实现了JSR-303 bean级(跨字段)验证。由于BeanValidationBinder不支持bean级验证,我们有自己的实现来验证这些跨字段约束,并在无效字段上显示验证错误消息 现在,这在其他用例中对我们很有效,但现在我在网格编辑器中显示字段的错误消息时遇到了问题 下面是一个简化示例,单击“保存”按钮时验证所有行。如果不是“产品”,则发现产品名称无效。只处理遇到的第一个错误,这很好 看起来这一切都归结为productGrid.getEditor(

我有一个用于编辑bean的网格,它为bean实现了JSR-303 bean级(跨字段)验证。由于BeanValidationBinder不支持bean级验证,我们有自己的实现来验证这些跨字段约束,并在无效字段上显示验证错误消息

现在,这在其他用例中对我们很有效,但现在我在网格编辑器中显示字段的错误消息时遇到了问题

下面是一个简化示例,单击“保存”按钮时验证所有行。如果不是“产品”,则发现产品名称无效。只处理遇到的第一个错误,这很好

看起来这一切都归结为productGrid.getEditor().editItem(p)-调用validate(List products)-方法。如果我省略它,错误消息将显示在当前可编辑字段中。当然,这可能不是包含无效数据的那个

起初,我确实尝试了一个更优雅的解决方案,用户不能离开无效的行,但现在我已经准备好解决这个问题,如果我能让它工作的话。我们在原始代码中使用了BenValidationBinder,它处理单字段JSR-303验证

通过编辑MainView类,我使用最新的“projectbase”应用程序starter()编写了示例代码,如下所示。使用Vaadin 13.0.3版运行

其他人也有同样的问题,希望能找到解决办法吗

@Route("")
@PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {

    private Grid<Product> productGrid;
    private TextField code = new TextField();
    private TextField name= new TextField();


    public MainView() {

        List<Product> products = Arrays.asList(new Product("1", "Product"), new Product("2", "Product"));

        productGrid = new Grid<MainView.Product>();
        productGrid.addColumn(Product::getCode)
        .setHeader("Product code")
        .setFlexGrow(1);
        productGrid.addColumn(Product::getName)
        .setEditorComponent(name)
        .setHeader("Product name")
        .setFlexGrow(1);

        Binder<Product> binder = new Binder<>(Product.class);
        binder.bindInstanceFields(this);
        productGrid.getEditor().setBinder(binder);

        productGrid.addItemClickListener(event -> productGrid.getEditor().editItem(event.getItem()));
        productGrid.setItems(products);
        Button save = new Button("Save",
                event -> save(products));

        add(save);
        add(productGrid);
    }



    private void save(List<Product> products) {
        if(validate(products)) {
            Notification.show("Saved: " + String.join(", ", products.stream().map(Product::toString).collect(Collectors.toList())));
        }else {
            Notification.show("Invalid data");
        }
    }

    private boolean validate(List<Product> products) {
        // this only mocks the JSR-303 validation call
        for(Product p:products) {
            if(!p.getName().equals("Product")) {
                productGrid.getEditor().editItem(p);
                name.setErrorMessage("Invalid name, should be: Product");
                name.setInvalid(true);
                return false;
            }
        }
        return true;
    }

    public class Product{

        String code;
        String name;

        public Product(String code, String name) {
            super();
            this.code = code;
            this.name = name;
        }
        public String getCode() {
            return code;
        }
        public void setCode(String code) {
            this.code = code;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public String toString() {
            return code + " " + name;
        }
    }

}

@Route(“”)
@PWA(name=“Vaadin流程的项目基础”,shortName=“项目基础”)
公共类主视图扩展了垂直布局{
私有网格;
私有TextField代码=新TextField();
private TextField name=new TextField();
公共主视图(){
列表产品=数组。asList(新产品(“1”、“产品”)、新产品(“2”、“产品”);
productGrid=新网格();
productGrid.addColumn(产品::getCode)
.setHeader(“产品代码”)
.setFlexGrow(1);
productGrid.addColumn(产品::getName)
.setEditorComponent(名称)
.setHeader(“产品名称”)
.setFlexGrow(1);
活页夹=新活页夹(产品类别);
binder.bindInstanceFields(本);
productGrid.getEditor().setBinder(活页夹);
productGrid.addItemClickListener(事件->productGrid.getEditor().editItem(事件.getItem());
productGrid.setItems(产品);
按钮保存=新建按钮(“保存”,
事件->保存(产品));
添加(保存);
添加(productGrid);
}
私有作废保存(列出产品){
if(验证(产品)){
Notification.show(“保存:”+String.join(“,”,products.stream().map(Product::toString.collect(Collectors.toList())));
}否则{
通知。显示(“无效数据”);
}
}
私有布尔验证(列出产品){
//这只模拟JSR-303验证调用
对于(产品p:产品){
如果(!p.getName().equals(“产品”)){
productGrid.getEditor().editItem(p);
name.setErrorMessage(“无效名称,应为:Product”);
name.setInvalid(true);
返回false;
}
}
返回true;
}
公共类产品{
字符串代码;
字符串名;
公共产品(字符串代码、字符串名称){
超级();
this.code=代码;
this.name=名称;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串toString(){
返回代码+“”+名称;
}
}
}