diff options
author | Artur Signell <artur@vaadin.com> | 2013-08-19 17:33:54 +0300 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2013-08-19 17:33:54 +0300 |
commit | 0a0077e06061c796bfb9f73179bac254cac8951e (patch) | |
tree | 3fce768372bba4024f5334170e2bff8362992a11 /server | |
parent | 633e0201cd2a79893d043c77b15062342cfe46e0 (diff) | |
parent | 11f8811eb0f7a306c403d36ab45f5d8658d7a164 (diff) | |
download | vaadin-framework-0a0077e06061c796bfb9f73179bac254cac8951e.tar.gz vaadin-framework-0a0077e06061c796bfb9f73179bac254cac8951e.zip |
Merge from origin/7.1
11f8811 Make sure bean field validators are only added once (#11045)
Conflicts:
server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java
Change-Id: I6533b00f78dfb2d587b52b320d2d709c640b9159
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/data/fieldgroup/BeanFieldGroup.java | 18 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java | 39 |
2 files changed, 56 insertions, 1 deletions
diff --git a/server/src/com/vaadin/data/fieldgroup/BeanFieldGroup.java b/server/src/com/vaadin/data/fieldgroup/BeanFieldGroup.java index 0b4e3a8049..cb8a044f00 100644 --- a/server/src/com/vaadin/data/fieldgroup/BeanFieldGroup.java +++ b/server/src/com/vaadin/data/fieldgroup/BeanFieldGroup.java @@ -16,6 +16,8 @@ package com.vaadin.data.fieldgroup; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; import com.vaadin.data.Item; import com.vaadin.data.util.BeanItem; @@ -27,9 +29,11 @@ public class BeanFieldGroup<T> extends FieldGroup { private Class<T> beanType; private static Boolean beanValidationImplementationAvailable = null; + private final Map<Field<?>, BeanValidator> defaultValidators; public BeanFieldGroup(Class<T> beanType) { this.beanType = beanType; + this.defaultValidators = new HashMap<Field<?>, BeanValidator>(); } @Override @@ -171,16 +175,28 @@ public class BeanFieldGroup<T> extends FieldGroup { } @Override + public void unbind(Field<?> field) throws BindException { + super.unbind(field); + + BeanValidator removed = defaultValidators.remove(field); + if (removed != null) { + field.removeValidator(removed); + } + } + + @Override protected void configureField(Field<?> field) { super.configureField(field); // Add Bean validators if there are annotations - if (isBeanValidationImplementationAvailable()) { + if (isBeanValidationImplementationAvailable() + && !defaultValidators.containsKey(field)) { BeanValidator validator = new BeanValidator(beanType, getPropertyId(field).toString()); field.addValidator(validator); if (field.getLocale() != null) { validator.setLocale(field.getLocale()); } + defaultValidators.put(field, validator); } } diff --git a/server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java b/server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java index b4409aed26..1d1a3c297e 100644 --- a/server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java +++ b/server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java @@ -4,8 +4,10 @@ import org.junit.Assert; import org.junit.Test; import com.vaadin.data.Validator.InvalidValueException; +import com.vaadin.data.fieldgroup.BeanFieldGroup; import com.vaadin.data.validator.BeanValidator; import com.vaadin.tests.data.bean.BeanToValidate; +import com.vaadin.ui.Field; public class TestBeanValidation { @Test(expected = InvalidValueException.class) @@ -82,4 +84,41 @@ public class TestBeanValidation { Assert.assertEquals(2, causes.length); } + public void testBeanValidationNotAddedTwice() { + // See ticket #11045 + BeanFieldGroup<BeanToValidate> fieldGroup = new BeanFieldGroup<BeanToValidate>( + BeanToValidate.class); + + BeanToValidate beanToValidate = new BeanToValidate(); + beanToValidate.setFirstname("a"); + fieldGroup.setItemDataSource(beanToValidate); + + Field<?> nameField = fieldGroup.buildAndBind("firstname"); + Assert.assertEquals(1, nameField.getValidators().size()); + + try { + nameField.validate(); + } catch (InvalidValueException e) { + // NOTE: causes are empty if only one validation fails + Assert.assertEquals(0, e.getCauses().length); + } + + // Create new, identical bean to cause duplicate validator unless #11045 + // is fixed + beanToValidate = new BeanToValidate(); + beanToValidate.setFirstname("a"); + fieldGroup.setItemDataSource(beanToValidate); + + Assert.assertEquals(1, nameField.getValidators().size()); + + try { + nameField.validate(); + } catch (InvalidValueException e) { + // NOTE: if more than one validation fails, we get the number of + // failed validations + Assert.assertEquals(0, e.getCauses().length); + } + + } + } |