Skip to content

Commit

Permalink
Remove removed binding from changedBinding field (#6827)
Browse files Browse the repository at this point in the history
Fixes #5384

(cherry picked from commit ccdd479)
  • Loading branch information
Denis authored and Joni Uitto committed Nov 7, 2019
1 parent 1764bca commit d0db756
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
10 changes: 7 additions & 3 deletions flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,11 @@ default BindingBuilder<BEAN, TARGET> withNullRepresentation(
TARGET nullRepresentation) {
return withConverter(
fieldValue -> Objects.equals(fieldValue, nullRepresentation)
? null : fieldValue,
? null
: fieldValue,
modelValue -> Objects.isNull(modelValue)
? nullRepresentation : modelValue);
? nullRepresentation
: modelValue);
}

/**
Expand Down Expand Up @@ -2478,7 +2480,8 @@ private <FIELDVALUE> Converter<FIELDVALUE, FIELDVALUE> createNullRepresentationA
Converter<FIELDVALUE, FIELDVALUE> nullRepresentationConverter = Converter
.from(fieldValue -> fieldValue,
modelValue -> Objects.isNull(modelValue)
? field.getEmptyValue() : modelValue,
? field.getEmptyValue()
: modelValue,
Throwable::getMessage);
ConverterDelegate<FIELDVALUE> converter = new ConverterDelegate<>(
nullRepresentationConverter);
Expand Down Expand Up @@ -2857,6 +2860,7 @@ protected void removeBindingInternal(Binding<BEAN, ?> binding) {
if (bindings.remove(binding)) {
boundProperties.entrySet()
.removeIf(entry -> entry.getValue().equals(binding));
changedBindings.remove(binding);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void setUp() {
binder = new Binder<Person>() {
@Override
protected void handleError(HasValue<?, ?> field,
ValidationResult result) {
ValidationResult result) {
super.handleError(field, result);
componentErrors.put(field, result.getErrorMessage());
}
Expand Down Expand Up @@ -110,6 +110,23 @@ public void bindNullBean_FieldsAreCleared() {
assertEquals("Age field not empty", "", ageField.getValue());
}

@Test
public void removeInvalidBinding_validateDoesNotThrow() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
Binding<Person, Integer> ageBinding = binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
binder.withValidator(bean -> true, "");
binder.setBean(item);

ageField.setValue("foo");

binder.removeBinding(ageBinding);

binder.validate();
}

@Test
public void clearForReadBean_boundFieldsAreCleared() {
binder.forField(nameField).bind(Person::getFirstName,
Expand Down Expand Up @@ -568,8 +585,7 @@ public void setRequired_withCustomValidator_fieldGetsRequiredIndicatorAndValidat
textField.setValue(" ");
String errorMessage = textField.getErrorMessage();
assertNotNull(errorMessage);
assertEquals("Input is required.",
componentErrors.get(textField));
assertEquals("Input is required.", componentErrors.get(textField));
// validation is done for all changed bindings once.
assertEquals(2, invokes.get());

Expand All @@ -586,7 +602,7 @@ public void setRequired_withCustomValidator_modelConverterBeforeValidator() {
Converter<String, String> stringBasicPreProcessingConverter = new Converter<String, String>() {
@Override
public Result<String> convertToModel(String value,
ValueContext context) {
ValueContext context) {
if (StringUtils.isBlank(value)) {
return Result.ok(null);
}
Expand All @@ -595,7 +611,7 @@ public Result<String> convertToModel(String value,

@Override
public String convertToPresentation(String value,
ValueContext context) {
ValueContext context) {
if (value == null) {
return "";
}
Expand Down

0 comments on commit d0db756

Please sign in to comment.