flow-components
flow-components copied to clipboard
DatePicker looses value with readBean(null) is invoked even if it is set later
Description of the bug
Hi,
We have a datepicker binded, if within the same request flow the binder readbean is invoked two times: readBean(null); readBean(myBean); The datepicker looeses value even if the myBean.getDateField() returns a valid localdate.
Expected behavior
If the second readBean object contains valid value for the binded field it should be filled for the DatePicker.
Minimal reproducible example
private TextField textField = new TextField();
private Button button = new Button("Test");
DatePicker datePick = new DatePicker();
Binder<MyBean> binder = new Binder<MyBean>(MyBean.class);
public HelloWorldView() {
add(textField, datePick, button);
binder.forField(textField).bind("name");
binder.forField(datePick).bind("localDate");
MyBean myBean = new MyBean();
myBean.setLocalDate(LocalDate.now());
myBean.setName("Test");
binder.readBean(myBean);
button.addClickListener(e -> {
MyBean myBean2 = new MyBean();
try {
binder.writeBean(myBean2);
} catch (ValidationException ex) {
throw new RuntimeException(ex);
}
binder.readBean(null);
binder.readBean(myBean2);
});
}
public static class MyBean {
private LocalDate localDate;
private String name;
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Versions
- Vaadin / Flow version: 24.6.7
- Java version: 21
The workaround is to postpone the second read after next client round trip using e.g.
button.addClickListener(e -> {
MyBean myBean2 = new MyBean();
try {
binder.writeBean(myBean2);
} catch (ValidationException ex) {
throw new RuntimeException(ex);
}
binder.readBean(null);
getElement().executeJs("setTimeout(function() { }, 0)").then(result -> binder.readBean(myBean2));
});
Seems to be the same issue as https://github.com/vaadin/flow-components/issues/7250