From 01c72e9e6c0678980bc39112f4b97454de6916c7 Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Thu, 18 Mar 2021 18:12:37 +0200 Subject: feat: Add API to control whether Binder converts back to presentation (#12246) #12132 fixed an old bug, conversion was not done back, which was a regression from Vaadin 7. However since the bug has been present long time, it has become established behavior, thus there may be need to disable conversion back to presentation sometimes, thus this API as added. --- server/src/main/java/com/vaadin/data/Binder.java | 43 ++++++++++++++++++++-- .../src/test/java/com/vaadin/data/BinderTest.java | 15 ++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) (limited to 'server/src') diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 8a11cc36ee..1fb64c837f 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -273,6 +273,28 @@ public class Binder implements Serializable { * @since 8.11 */ public boolean isValidatorsDisabled(); + + /** + * Define whether the value should be converted back to the presentation + * in the field when a converter is used in binding. + * + * @param convertBackToPresentation + * A boolean value + * + * @since 8.13 + */ + public void setConvertBackToPresentation( + boolean convertBackToPresentation); + + /** + * Returns whether the value is converted back to the presentation in + * the field when a converter is used in binding. + * + * @return A boolean value + * + * @since 8.13 + */ + public boolean isConvertBackToPresentation(); } /** @@ -1092,6 +1114,8 @@ public class Binder implements Serializable { private boolean validatorsDisabled = false; + private boolean convertBackToPresentation = true; + public BindingImpl(BindingBuilderImpl builder, ValueProvider getter, Setter setter) { @@ -1272,10 +1296,12 @@ public class Binder implements Serializable { /** * Write the field value by invoking the setter function on the given - * bean, if the value passes all registered validators. + * bean, if the value passes all registered validators. Write value back + * to the field as well if {@code isConvertBackToPresentation()} is + * true. * * @param bean - * the bean to set the property value to + * the bean to set the property value to, not null */ private BindingValidationStatus writeFieldValue(BEAN bean) { assert bean != null; @@ -1284,7 +1310,7 @@ public class Binder implements Serializable { if (!isReadOnly()) { result.ifOk(value -> { setter.accept(bean, value); - if (value != null) { + if (convertBackToPresentation && value != null) { FIELDVALUE converted = convertToFieldType(value); if (!Objects.equals(field.getValue(), converted)) { getField().setValue(converted); @@ -1392,6 +1418,17 @@ public class Binder implements Serializable { public boolean isValidatorsDisabled() { return validatorsDisabled; } + + @Override + public void setConvertBackToPresentation( + boolean convertBackToPresentation) { + this.convertBackToPresentation = convertBackToPresentation; + } + + @Override + public boolean isConvertBackToPresentation() { + return convertBackToPresentation; + } } /** diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index 0332572203..98f72e1197 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -517,6 +517,21 @@ public class BinderTest extends BinderTestBase, Person> { assertEquals("€ 10.00", rentField.getValue()); } + @Test + public void withConverter_writeBackValueDisabled() { + TextField rentField = new TextField(); + rentField.setValue(""); + Binding binding = binder.forField(rentField) + .withConverter(new EuroConverter("")) + .withNullRepresentation(BigDecimal.valueOf(0d)) + .bind(Person::getRent, Person::setRent); + binder.setBean(item); + binding.setConvertBackToPresentation(false); + rentField.setValue("10"); + + assertNotEquals("€ 10.00", rentField.getValue()); + } + @Test public void beanBinder_nullRepresentationIsNotDisabled() { Binder binder = new Binder<>(Person.class); -- cgit v1.2.3