为了账号安全,请及时绑定邮箱和手机立即绑定

如何在瓦丁中使用状态更改通知程序进行验证?

如何在瓦丁中使用状态更改通知程序进行验证?

陪伴而非守候 2022-09-21 17:36:29
我正在使用绑定器来绑定和验证 和 .为了获得验证更改的通知,我向绑定程序添加了一个状态更改通知程序。侦听器检查是否返回 false。但是,在组合框中选择有效条目但文本字段中的无效条目后,它将返回 false。因此,即使存在验证错误,它也返回 false。有关最小示例,请参见下文。TextFieldComboBox.hasValidationErrors()public class TestWindow extends Window {    private final Binder<State> binder;    public TestWindow() {        this.binder  = new Binder<>();        ComboBox<String> comboBox = new ComboBox<>("comboBox", List.of("A", "B"));        TextField textField = new TextField("textField");        this.binder.forField(comboBox).bind(State::getComboBox, State::setComboBox);        this.binder.forField(textField)                .withValidator(string -> string.length() > 3, "tmp")                .bind(State::getName, State::setName);        this.binder.addStatusChangeListener( status -> System.err.println(status.hasValidationErrors()));        setContent(new VerticalLayout(comboBox, textField));    }    private class State {        private String name;        private String comboBox;        public State(String name, String comboBox) {            this.name = name;            this.comboBox = comboBox;        }        public String getComboBox() {            return this.comboBox;        }        public void setComboBox(String comboBox) {            this.comboBox = comboBox;        }        public String getName() {            return this.name;        }        public void setName(String name) {            this.name = name;        }    }}在文本字段中输入一个太短的字符串并在组合框中选择某些内容后,我希望打印出来。true
查看完整描述

1 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

您只是在检查最近更改的组件的值是否有效。如果要检查绑定组件是否存在任何验证错误,请使用 。binder.isValid()

 binder.addStatusChangeListener(status -> System.err.println(binder.isValid()));

请注意,您的布尔值现在是反转的。

您可以在官方文档中找到很多有用的示例:将数据绑定到表单


查看完整回答
反对 回复 2022-09-21
  • 1 回答
  • 0 关注
  • 62 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信