如何在Vaadin中使用StatusChangeListener进行验证?

时间:2019-04-03 16:22:15

标签: java vaadin

我正在使用活页夹来绑定和验证TextFieldComboBox。为了获得有关验证更改的通知,我将StatusChangeListener添加到活页夹中。侦听器检查.hasValidationErrors()是否返回false。但是,在组合框中选择一个有效的条目,但在文本字段中选择一个无效的条目后,它返回false。因此,即使存在验证错误,它也会返回false。 参见下面的最小示例。

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 个答案:

答案 0 :(得分:2)

您只是在检查最近更改的组件的值是否有效。如果要检查绑定组件是否存在任何验证错误,请使用struct generator { char r[26][max]; void set(); void display(); }; void generator::set() { char *tab = new char[max]; int k = 0; cin >> tab; while (tab[k] != '\0') { r[0][k] = tab[k]; k++; } void generator::display(){ cout << r[0][1]; // should display first letter of string }

let array = [
 {
   "aptNumber": "",
   "city": "",
   ...   
 },
 {
   "aptNumber": "",
   "city": "",
   ...    
 }
]

console.log(array.map(x => x.key));

请注意,您的布尔值现在已经反转了。

您可以在官方文档中找到许多有用的示例: Binding Data to Forms