Skip to content

Commit 4e36fd9

Browse files
authored
fix: Ensure that side effects of readBean do not set hasChanges true (#12181)
JavaDoc of hasChanges says: "Check whether any of the bound fields' have uncommitted changes since last explicit call to readBean(Object), removeBean(), writeBean(Object)} or writeBeanIfValid(Object)." If readBean has converters, they will be run and field values updated accordingly. Furthermore if fields have value change listeners that will produce further changes in values, this should be considered according to above as part of readBean procedure and thus hasChanges still should return false. Cherry pick from: vaadin/framework#12455
1 parent ad39b06 commit 4e36fd9

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,6 @@ public void readBean(BEAN bean) {
21012101
if (bean == null) {
21022102
clearFields();
21032103
} else {
2104-
changedBindings.clear();
21052104
getBindings().forEach(binding -> {
21062105
/*
21072106
* Some bindings may have been removed from binder during
@@ -2113,6 +2112,7 @@ public void readBean(BEAN bean) {
21132112
binding.initFieldValue(bean, false);
21142113
}
21152114
});
2115+
changedBindings.clear();
21162116
getValidationStatusHandler().statusChange(
21172117
BinderValidationStatus.createUnresolvedStatus(this));
21182118
fireStatusChangeEvent(false);

‎flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,29 @@ private void setExceptionHandler() {
19681968
(field, exception) -> Optional.of(bindingException));
19691969
}
19701970

1971+
// See: https://github.com/vaadin/framework/issues/9581
1972+
@Test
1973+
public void withConverter_hasChangesFalse() {
1974+
TestTextField nameField = new TestTextField();
1975+
nameField.setValue("");
1976+
TestTextField rentField = new TestTextField();
1977+
rentField.setValue("");
1978+
rentField.addValueChangeListener(event -> {
1979+
nameField.setValue("Name");
1980+
});
1981+
item.setRent(BigDecimal.valueOf(10));
1982+
binder.forField(nameField).bind(Person::getFirstName,
1983+
Person::setFirstName);
1984+
binder.forField(rentField).withConverter(new EuroConverter(""))
1985+
.withNullRepresentation(BigDecimal.valueOf(0d))
1986+
.bind(Person::getRent, Person::setRent);
1987+
binder.readBean(item);
1988+
1989+
assertFalse(binder.hasChanges());
1990+
assertEquals("€ 10.00", rentField.getValue());
1991+
assertEquals("Name", nameField.getValue());
1992+
}
1993+
19711994
private TestTextField createNullRejectingFieldWithEmptyValue(
19721995
String emptyValue) {
19731996
return new TestTextField() {

0 commit comments

Comments
 (0)