diff options
author | Marco Collovati <mcollovati@gmail.com> | 2017-12-29 10:36:38 +0100 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2017-12-29 11:36:38 +0200 |
commit | 1619d1f0bbd41de3f175b9eb2f34b468ec7e0367 (patch) | |
tree | c8461336b55cfb9b091feb98510345ec3e13fe08 | |
parent | a0150a9899afd1d6e1251d9281262eddafd1d417 (diff) | |
download | vaadin-framework-1619d1f0bbd41de3f175b9eb2f34b468ec7e0367.tar.gz vaadin-framework-1619d1f0bbd41de3f175b9eb2f34b468ec7e0367.zip |
Treat fields as readonly when bound with null setter (#10477)
Fixes #10476
4 files changed, 41 insertions, 8 deletions
diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 26354138ce..8f25e92aba 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -228,6 +228,10 @@ public class Binder<BEAN> implements Serializable { * binder.forField(nameField).bind(Person::getName, Person::setName); * </pre> * + * <p> + * <strong>Note:</strong> when a {@code null} setter is given the field will be + * marked as readonly by invoking ({@link HasValue::setReadOnly}. + * * @param getter * the function to get the value of the property to the * field, not null @@ -256,6 +260,10 @@ public class Binder<BEAN> implements Serializable { * an accessible setter; in that case the property value is never * updated and the binding is said to be <i>read-only</i>. * + * <p> + * <strong>Note:</strong> when the binding is <i>read-only</i> the field will be + * marked as readonly by invoking ({@link HasValue::setReadOnly}. + * * @param propertyName * the name of the property to bind, not null * @return the newly created binding @@ -741,6 +749,9 @@ public class Binder<BEAN> implements Serializable { if (getBinder().getBean() != null) { binding.initFieldValue(getBinder().getBean()); } + if (setter == null) { + binding.getField().setReadOnly(true); + } getBinder().fireStatusChangeEvent(false); bound = true; @@ -1444,6 +1455,10 @@ public class Binder<BEAN> implements Serializable { * binder.bind(nameField, Person::getName, Person::setName); * </pre> * + * <p> + * <strong>Note:</strong> when a {@code null} setter is given the field will be + * marked as readonly by invoking ({@link HasValue::setReadOnly}. + * * @param <FIELDVALUE> * the value type of the field * @param field diff --git a/server/src/test/java/com/vaadin/data/BeanBinderTest.java b/server/src/test/java/com/vaadin/data/BeanBinderTest.java index 567bdcc3e8..e6740f35ba 100644 --- a/server/src/test/java/com/vaadin/data/BeanBinderTest.java +++ b/server/src/test/java/com/vaadin/data/BeanBinderTest.java @@ -311,21 +311,28 @@ public class BeanBinderTest } @Test + public void bindReadOnlyPropertyShouldMarkFieldAsReadonly() { + binder.bind(nameField, "readOnlyProperty"); + + assertTrue("Name field should be readonly", nameField.isReadOnly()); + } + + @Test public void setReadonlyShouldIgnoreBindingsForReadOnlyProperties() { binder.bind(nameField, "readOnlyProperty"); binder.setReadOnly(true); - assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); + assertTrue("Name field should be ignored and be readonly", nameField.isReadOnly()); binder.setReadOnly(false); - assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); + assertTrue("Name field should be ignored and be readonly", nameField.isReadOnly()); - nameField.setReadOnly(true); + nameField.setReadOnly(false); binder.setReadOnly(true); - assertTrue("Name field should be ignored and be readonly", nameField.isReadOnly()); + assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); binder.setReadOnly(false); - assertTrue("Name field should be ignored and be readonly", nameField.isReadOnly()); + assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); } @Test diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index 5c6c9657a5..131dcb29b0 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -616,11 +616,11 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> { .bind(Person::getAge, Person::setAge); binder.setReadOnly(true); - assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); + assertTrue("Name field should be ignored but should be readonly", nameField.isReadOnly()); assertTrue("Age field should be readonly", ageField.isReadOnly()); binder.setReadOnly(false); - assertFalse("Name field should be ignored and remain not readonly", nameField.isReadOnly()); + assertTrue("Name field should be ignored and should remain readonly", nameField.isReadOnly()); assertFalse("Age field should not be readonly", ageField.isReadOnly()); nameField.setReadOnly(false); @@ -1044,4 +1044,15 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> { Person::getFirstName, Person::setFirstName); binder.removeBinding(binding); } + + @Test + public void bindWithNullSetterShouldMarkFieldAsReadonly() { + binder.bind(nameField, Person::getFirstName, null); + binder.forField(ageField) + .withConverter(new StringToIntegerConverter("")) + .bind(Person::getAge, Person::setAge); + + assertTrue("Name field should be readonly", nameField.isReadOnly()); + assertFalse("Name field should be readonly", ageField.isReadOnly()); + } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/datefield/DateTextHandling.java b/uitest/src/main/java/com/vaadin/tests/components/datefield/DateTextHandling.java index 8dd7ebbb58..c0e15be7eb 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/datefield/DateTextHandling.java +++ b/uitest/src/main/java/com/vaadin/tests/components/datefield/DateTextHandling.java @@ -45,7 +45,7 @@ public class DateTextHandling extends AbstractTestUI { layout.addComponent(errorLabel); Binder<Void> binder = new Binder<>(); - binder.forField(dateField).withStatusLabel(errorLabel).bind(o -> dateField.getValue(), null); + binder.forField(dateField).withStatusLabel(errorLabel).bind(o -> dateField.getValue(), (aVoid, date) -> {}); Button buttonValidate = new Button("Validate!"); buttonValidate.addClickListener(event1 -> { |