Skip to content

Commit

Permalink
fix: Add API to control whether Binder converts back to presentation …
Browse files Browse the repository at this point in the history
…(CP: #10343)
  • Loading branch information
knoobie authored and pleku committed Mar 24, 2021
1 parent 9389ef4 commit a91d504
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
58 changes: 57 additions & 1 deletion flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java
Expand Up @@ -250,6 +250,32 @@ default BindingValidationStatus<TARGET> validate() {
* @return A boolean value.
*/
public boolean isValidatorsDisabled();

/**
* Define whether the value should be converted back to the presentation
* in the field when a converter is used in binding.
* <p>
* As of version 6.0, when a converter is used on a binding and the user
* input value is modified by the converter, the value from the converter
* is applied back to the input. It is possible to control this behavior
* with this API.
*
* @see BindingBuilder#withConverter(Converter)
* @see BindingBuilder#withConverter(SerializableFunction, SerializableFunction)
* @see BindingBuilder#withConverter(SerializableFunction, SerializableFunction, String)
*
* @param convertBackToPresentation
* A boolean value
*/
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
*/
boolean isConvertBackToPresentation();
}

/**
Expand Down Expand Up @@ -508,6 +534,12 @@ default BindingBuilder<BEAN, TARGET> withValidator(
* For instance, a {@code TextField} can be bound to an integer-typed
* property using an appropriate converter such as a
* {@link StringToIntegerConverter}.
* <p>
* The converted value is applied back to the field by default,
* this can be controlled with the method
* {@link Binding#setConvertBackToPresentation(boolean)}.
*
* @see Binding#setConvertBackToPresentation(boolean)
*
* @param <NEWTARGET>
* the type to convert to
Expand All @@ -534,6 +566,12 @@ <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
* For instance, a {@code TextField} can be bound to an integer-typed
* property using appropriate functions such as:
* <code>withConverter(Integer::valueOf, String::valueOf);</code>
* <p>
* The converted value is applied back to the field by default,
* this can be controlled with the method
* {@link Binding#setConvertBackToPresentation(boolean)}.
*
* @see Binding#setConvertBackToPresentation(boolean)
*
* @param <NEWTARGET>
* the type to convert to
Expand Down Expand Up @@ -569,6 +607,12 @@ default <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
* For instance, a {@code TextField} can be bound to an integer-typed
* property using appropriate functions such as:
* <code>withConverter(Integer::valueOf, String::valueOf);</code>
* <p>
* The converted value is applied back to the field by default,
* this can be controlled with the method
* {@link Binding#setConvertBackToPresentation(boolean)}.
*
* @see Binding#setConvertBackToPresentation(boolean)
*
* @param <NEWTARGET>
* the type to convert to
Expand Down Expand Up @@ -1055,6 +1099,8 @@ protected static class BindingImpl<BEAN, FIELDVALUE, TARGET>

private boolean validatorsDisabled = false;

private boolean convertBackToPresentation = true;

public BindingImpl(BindingBuilderImpl<BEAN, FIELDVALUE, TARGET> builder,
ValueProvider<BEAN, TARGET> getter,
Setter<BEAN, TARGET> setter) {
Expand Down Expand Up @@ -1246,7 +1292,7 @@ private BindingValidationStatus<TARGET> writeFieldValue(BEAN bean) {
if (!isReadOnly()) {
result.ifOk(value -> {
setter.accept(bean, value);
if (value != null) {
if (convertBackToPresentation && value != null) {
FIELDVALUE converted = convertToFieldType(value);
if (!field.getValue().equals(converted)) {
getField().setValue(converted);
Expand Down Expand Up @@ -1353,6 +1399,16 @@ public void setValidatorsDisabled(boolean validatorsDisabled) {
public boolean isValidatorsDisabled() {
return validatorsDisabled;
}

@Override
public void setConvertBackToPresentation(boolean convertBackToPresentation) {
this.convertBackToPresentation = convertBackToPresentation;
}

@Override
public boolean isConvertBackToPresentation() {
return convertBackToPresentation;
}
}

/**
Expand Down
Expand Up @@ -583,7 +583,22 @@ public void withConverter_writeBackValue() {
// € 10.00
assertEquals("€ " + new DecimalFormat("0.00").format(10),
rentField.getValue());
}
}

@Test
public void withConverter_writeBackValueDisabled() {
TestTextField rentField = new TestTextField();
rentField.setValue("");
Binding<Person, BigDecimal> 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() {
Expand Down

0 comments on commit a91d504

Please sign in to comment.