flow: Validation status reported on UI is not in sync with the binder

With following code snippet:

  1. write a letter to input
  2. delete the letter from input
  3. click the button
  4. binder reports valid bean, UI shows invalid value in the TextField

Test code:


import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.router.Route;
import javax.validation.constraints.NotNull;
import java.util.Set;

@Route("")
public class MainView extends VerticalLayout {

    public static class Entity {

        @NotNull
        private String name;

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return "Entity, name: " + name;
        }

    }

    final TextField name = new TextField();

    public MainView() {

        final VerticalLayout layout = new VerticalLayout();
        name.setLabel("Type your name here:");

        Entity entity = new Entity();
        Binder<Entity> b = new BeanValidationBinder<>(Entity.class);
        b.bindInstanceFields(this);

        b.setBean(entity);

        Button button = new Button("Check validity");
        button.addClickListener(e -> {
            String msg = String.format("Binder.isValid: %s", b.isValid());
            Notification.show(msg);
        });

        add(name, button);

    }

}

Note, the code snippet is related to https://github.com/vaadin/framework/issues/9000 (which is valid for Flow as well), but the issue here is different.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20 (20 by maintainers)

Commits related to this issue

Most upvoted comments

I think it would be fine to make setRequiredIndicatorVisible(true) overwrite checkValidity() to something that only looks at the invalid property, and then setRequiredIndicatorVisible(false) removes that overwrite.

If you’re using e.g. <vaadin-text-field> without Binder and want to make it required, then you should use the TextField::setRequired method instead of setRequiredIndicatorVisible.