Skip to content

Commit 57bfc5e

Browse files
authored
fix: Avoid processing value change event due writing back of converted value (#12183)
1 parent 4f62532 commit 57bfc5e

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ protected static class BindingImpl<BEAN, FIELDVALUE, TARGET>
11881188

11891189
private Registration onValueChange;
11901190
private boolean valueInit = false;
1191+
private boolean convertedBack = false;
11911192

11921193
/**
11931194
* Contains all converters and validators chained together in the
@@ -1373,7 +1374,8 @@ private FIELDVALUE convertToFieldType(TARGET target) {
13731374
private void handleFieldValueChange(
13741375
ValueChangeEvent<FIELDVALUE> event) {
13751376
// Don't handle change events when setting initial value
1376-
if (valueInit) {
1377+
if (valueInit || convertedBack) {
1378+
convertedBack = false;
13771379
return;
13781380
}
13791381

@@ -1402,6 +1404,7 @@ private BindingValidationStatus<TARGET> writeFieldValue(BEAN bean) {
14021404
if (convertBackToPresentation && value != null) {
14031405
FIELDVALUE converted = convertToFieldType(value);
14041406
if (!Objects.equals(field.getValue(), converted)) {
1407+
convertedBack = true;
14051408
getField().setValue(converted);
14061409
}
14071410
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.vaadin.flow.data.binder.testcomponents.TestTextField;
4848
import com.vaadin.flow.data.converter.Converter;
4949
import com.vaadin.flow.data.converter.StringToBigDecimalConverter;
50+
import com.vaadin.flow.data.converter.StringToDoubleConverter;
5051
import com.vaadin.flow.data.converter.StringToIntegerConverter;
5152
import com.vaadin.flow.data.validator.IntegerRangeValidator;
5253
import com.vaadin.flow.data.validator.NotEmptyValidator;
@@ -1742,6 +1743,35 @@ public void invalidUsage_modifyFieldsInsideValidator_binderDoesNotThrow() {
17421743
Assert.assertTrue(validatorIsExecuted.get());
17431744
}
17441745

1746+
@Test
1747+
public void validationShouldNotRunTwice() {
1748+
TestTextField salaryField = new TestTextField();
1749+
AtomicInteger count = new AtomicInteger(0);
1750+
item.setSalaryDouble(100d);
1751+
binder.forField(salaryField)
1752+
.withConverter(new StringToDoubleConverter(""))
1753+
.bind(Person::getSalaryDouble, Person::setSalaryDouble);
1754+
binder.setBean(item);
1755+
binder.addValueChangeListener(event -> {
1756+
count.incrementAndGet();
1757+
});
1758+
1759+
salaryField.setValue("1000");
1760+
assertTrue(binder.isValid());
1761+
assertEquals(1, count.get());
1762+
1763+
salaryField.setValue("salary");
1764+
assertFalse(binder.isValid());
1765+
assertEquals(2, count.get());
1766+
1767+
salaryField.setValue("2000");
1768+
1769+
// Without fix for #12356 count will be 5
1770+
assertEquals(3, count.get());
1771+
1772+
assertEquals(new Double(2000), item.getSalaryDouble());
1773+
}
1774+
17451775
@Test
17461776
public void setValidationErrorHandler_handlerIsSet_handlerMethodsAreCalled() {
17471777
TestTextField testField = new TestTextField();

0 commit comments

Comments
 (0)