I’ve got a question about a change in the behavior of binders after upgrading our dependencies from Vaadin 8.3.3 to 8.4.0 (and later).
From 8.4.0 and onwards, setting Binder.setReadOnly(true) and afterwards AbstractField.setReadOnly(false) results in a value change event being fired but the bean in the binder not being changed.
In 8.3.3 the behavior has been different: In the mentioned case, a value change event would have been fired and the bean itself would have been changed as well.
Is this a planned change in behavior? I’m asking because I could not find a relevant release note mentioning this change.
I have added a minimal working example below:
public class TestDesign extends VerticalLayout {
protected Button btnPrint;
protected DateField date;
protected DateTimeField datetime;
public TestDesign() {
Design.read(this);
}
}
import java.time.LocalDate;
import java.time.LocalDateTime;
import javax.annotation.PostConstruct;
import com.vaadin.data.Binder;
import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.spring.annotation.SpringView;
import com.vaadin.spring.annotation.UIScope;
@SpringView(name = “test”)
@UIScope
@SuppressWarnings({“serial”, “nls”})
public class TestView extends TestDesign implements View {
protected final Binder<TestPojo> binder = new Binder<>(TestPojo.class);
public TestView() {
bind();
}
private void bind() {
binder.forMemberField(date);
binder.forMemberField(datetime);
binder.addValueChangeListener(TestView::showChanges);
binder.bindInstanceFields(this);
// binder.setReadOnly(true); // setting this to true will always change changes to the bean, even if AbstractField.setReadOnly(false) is called afterwards
TestPojo pojo = new TestPojo();
binder.setBean(pojo);
}
@PostConstruct
private void initComponents() {
date.setReadOnly(false);
datetime.setReadOnly(false);
btnPrint.addClickListener(event -> printPojoValues());
}
private void printPojoValues() {
TestPojo pojo = binder.getBean();
System.out.println("Bean:");
System.out.println("date: " + pojo.getDate());
System.out.println("datetime: " + pojo.getDatetime());
}
private static <T> void showChanges(ValueChangeEvent<T> event) {
System.out.println("Event:");
System.out.println("user? " + event.isUserOriginated());
System.out.println("old: " + event.getOldValue());
System.out.println("new: " + event.getValue());
}
public static class TestPojo {
private LocalDate date;
private LocalDateTime datetime;
public LocalDate getDate() {
return date;
}
public LocalDateTime getDatetime() {
return datetime;
}
public void setDate(LocalDate date) {
this.date = date;
}
public void setDatetime(LocalDateTime datetime) {
this.datetime = datetime;
}
}
}