flow: Validation status reported on UI is not in sync with the binder
With following code snippet:
- write a letter to input
- delete the letter from input
- click the button
- 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
- Clarify javadocs for setRequiredIndoctor method Fix for #4077 — committed to vaadin/flow by deleted user 6 years ago
- Clarify javadocs for setRequiredIndoctor method (#4149) Fix for #4077 — committed to vaadin/flow by deleted user 6 years ago
I think it would be fine to make
setRequiredIndicatorVisible(true)overwritecheckValidity()to something that only looks at theinvalidproperty, and thensetRequiredIndicatorVisible(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 theTextField::setRequiredmethod instead ofsetRequiredIndicatorVisible.