我正在使用綁定器來綁定和驗證 和 .為了獲得驗證更改的通知,我向綁定程序添加了一個狀態更改通知程序。偵聽器檢查是否返回 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()));
請注意,您的布爾值現在是反轉的。
您可以在官方文檔中找到很多有用的示例:將數據綁定到表單
添加回答
舉報
0/150
提交
取消