소스 검색

Treat fields as readonly when bound with null setter (#10477)

Fixes #10476
tags/8.3.0.alpha1
Marco Collovati 6 년 전
부모
커밋
1619d1f0bb

+ 15
- 0
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

+ 12
- 5
server/src/test/java/com/vaadin/data/BeanBinderTest.java 파일 보기

@@ -310,22 +310,29 @@ public class BeanBinderTest
assertEquals(propertyValue, item.getReadOnlyProperty());
}

@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

+ 13
- 2
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());
}
}

+ 1
- 1
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 -> {

Loading…
취소
저장