From d0db756be16e95d4846bcfc890c9b51e0294b944 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 6 Nov 2019 10:55:28 +0200 Subject: [PATCH] Remove removed binding from changedBinding field (#6827) Fixes #5384 (cherry picked from commit ccdd479072c55aaf5dee2d87a26edbb99711c8be) --- .../com/vaadin/flow/data/binder/Binder.java | 10 ++++--- .../vaadin/flow/data/binder/BinderTest.java | 26 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java index 76c0532f238..998caac5da8 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java @@ -566,9 +566,11 @@ default BindingBuilder withNullRepresentation( TARGET nullRepresentation) { return withConverter( fieldValue -> Objects.equals(fieldValue, nullRepresentation) - ? null : fieldValue, + ? null + : fieldValue, modelValue -> Objects.isNull(modelValue) - ? nullRepresentation : modelValue); + ? nullRepresentation + : modelValue); } /** @@ -2478,7 +2480,8 @@ private Converter createNullRepresentationA Converter nullRepresentationConverter = Converter .from(fieldValue -> fieldValue, modelValue -> Objects.isNull(modelValue) - ? field.getEmptyValue() : modelValue, + ? field.getEmptyValue() + : modelValue, Throwable::getMessage); ConverterDelegate converter = new ConverterDelegate<>( nullRepresentationConverter); @@ -2857,6 +2860,7 @@ protected void removeBindingInternal(Binding binding) { if (bindings.remove(binding)) { boundProperties.entrySet() .removeIf(entry -> entry.getValue().equals(binding)); + changedBindings.remove(binding); } } diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java index cfe268a905b..279a01cd0f5 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java @@ -64,7 +64,7 @@ public void setUp() { binder = new Binder() { @Override protected void handleError(HasValue field, - ValidationResult result) { + ValidationResult result) { super.handleError(field, result); componentErrors.put(field, result.getErrorMessage()); } @@ -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 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, @@ -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()); @@ -586,7 +602,7 @@ public void setRequired_withCustomValidator_modelConverterBeforeValidator() { Converter stringBasicPreProcessingConverter = new Converter() { @Override public Result convertToModel(String value, - ValueContext context) { + ValueContext context) { if (StringUtils.isBlank(value)) { return Result.ok(null); } @@ -595,7 +611,7 @@ public Result convertToModel(String value, @Override public String convertToPresentation(String value, - ValueContext context) { + ValueContext context) { if (value == null) { return ""; }