Browse Source

Clear errors when clearing binder fields (#9364)

Calling readBean(null) should reset the form state to the initial state
it was in before calling writeBean(bean)
tags/8.1.0.beta1
Artur 7 years ago
parent
commit
91e34500d7

+ 4
- 1
server/src/main/java/com/vaadin/data/Binder.java View File

@@ -1561,7 +1561,10 @@ public class Binder<BEAN> implements Serializable {
* Clear all the bound fields for this binder.
*/
private void clearFields() {
bindings.forEach(binding -> binding.getField().clear());
bindings.forEach(binding -> {
binding.getField().clear();
clearError(binding.getField());
});
if (hasChanges()) {
fireStatusChangeEvent(false);
}

+ 20
- 0
server/src/test/java/com/vaadin/data/BinderTest.java View File

@@ -441,6 +441,26 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
assertTrue(textField.isRequiredIndicatorVisible());
}

@Test
public void readNullBeanRemovesError() {
TextField textField = new TextField();
binder.forField(textField).asRequired("foobar")
.bind(Person::getFirstName, Person::setFirstName);
Assert.assertTrue(textField.isRequiredIndicatorVisible());
Assert.assertNull(textField.getErrorMessage());

binder.readBean(item);
Assert.assertNull(textField.getErrorMessage());

textField.setValue(textField.getEmptyValue());
Assert.assertTrue(textField.isRequiredIndicatorVisible());
Assert.assertNotNull(textField.getErrorMessage());

binder.readBean(null);
assertTrue(textField.isRequiredIndicatorVisible());
Assert.assertNull(textField.getErrorMessage());
}

@Test
public void setRequired_withErrorMessageProvider_fieldGetsRequiredIndicatorAndValidator() {
TextField textField = new TextField();

Loading…
Cancel
Save