From dd806e8bb31ecc7bce50f3aed5ed3fab48364895 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Wed, 11 Oct 2017 15:15:46 +0300 Subject: Fix Binder bean writing to only validate and write given bindings (#10162) --- .../vaadin/data/BinderConverterValidatorTest.java | 12 ++++++-- .../src/test/java/com/vaadin/data/BinderTest.java | 36 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) (limited to 'server/src/test/java') diff --git a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java index 4915bc9a5b..734aa5b194 100644 --- a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java @@ -621,14 +621,22 @@ public class BinderConverterValidatorTest // bind a new field that has invalid value in bean TextField lastNameField = new TextField(); - person.setLastName(""); + + // The test starts with a valid value as the last name of the person, + // since the binder assumes any non-changed values to be valid. + person.setLastName("bar"); + BindingBuilder binding2 = binder.forField(lastNameField) .withValidator(notEmpty); binding2.bind(Person::getLastName, Person::setLastName); - // should not have error shown + // should not have error shown when initialized assertNull(lastNameField.getComponentError()); + // Set a value that breaks the validation + lastNameField.setValue(""); + assertNotNull(lastNameField.getComponentError()); + // add status label to show bean level error Label statusLabel = new Label(); binder.setStatusLabel(statusLabel); diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index 1169eb8a5d..a44e3b3e06 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -392,9 +392,8 @@ public class BinderTest extends BinderTestBase, Person> { String customNullPointerRepresentation = "foo"; Binder binder = new Binder<>(Person.class); binder.forField(nameField) - .withConverter(value -> value, - value -> value == null ? customNullPointerRepresentation - : value) + .withConverter(value -> value, value -> value == null + ? customNullPointerRepresentation : value) .bind("firstName"); Person person = new Person(); @@ -490,7 +489,7 @@ public class BinderTest extends BinderTestBase, Person> { ErrorMessage errorMessage = textField.getErrorMessage(); assertNotNull(errorMessage); assertEquals("foobar", errorMessage.getFormattedHtmlMessage()); - // validation is done for the whole bean at once. + // validation is done for all changed bindings once. assertEquals(1, invokes.get()); textField.setValue("value"); @@ -942,4 +941,33 @@ public class BinderTest extends BinderTestBase, Person> { assertEquals("Binding still affects bean even after unbind", ageBeforeUnbind, String.valueOf(item.getAge())); } + + @Test + public void two_asRequired_fields_without_initial_values() { + binder.forField(nameField).asRequired("Empty name").bind(p -> "", + (p, s) -> { + }); + binder.forField(ageField).asRequired("Empty age").bind(p -> "", + (p, s) -> { + }); + + binder.setBean(item); + assertNull("Initially there should be no errors", + nameField.getComponentError()); + assertNull("Initially there should be no errors", + ageField.getComponentError()); + + nameField.setValue("Foo"); + assertNull("Name with a value should not be an error", + nameField.getComponentError()); + assertNull( + "Age field should not be in error, since it has not been modified.", + ageField.getComponentError()); + + nameField.setValue(""); + assertNotNull("Empty name should now be in error.", + nameField.getComponentError()); + assertNull("Age field should still be ok.", + ageField.getComponentError()); + } } -- cgit v1.2.3