From bcd7259e1092badf9abc6aa2146decdcc4611f14 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pekka=20Hyv=C3=B6nen?= Date: Mon, 31 Oct 2016 12:15:20 +0200 Subject: [PATCH] Remove Locale from BeanValidator because of ValueContext Fixes vaadin/framework8-issues#417 Change-Id: I0d299cb040cc21e9708906f12f4133109f1b2a90 --- .../main/java/com/vaadin/data/BeanBinder.java | 8 +-- .../vaadin/data/validator/BeanValidator.java | 61 +++++-------------- .../data/validator/BeanValidatorTest.java | 10 +-- 3 files changed, 25 insertions(+), 54 deletions(-) diff --git a/server/src/main/java/com/vaadin/data/BeanBinder.java b/server/src/main/java/com/vaadin/data/BeanBinder.java index ab87643451..b05e24fc9c 100644 --- a/server/src/main/java/com/vaadin/data/BeanBinder.java +++ b/server/src/main/java/com/vaadin/data/BeanBinder.java @@ -187,8 +187,8 @@ public class BeanBinder extends Binder { finalBinding = withConverter(createConverter(), false); if (BeanUtil.checkBeanValidationAvailable()) { - finalBinding = finalBinding.withValidator(new BeanValidator( - getBinder().beanType, propertyName, findLocale())); + finalBinding = finalBinding.withValidator( + new BeanValidator(getBinder().beanType, propertyName)); } PropertyDescriptor descriptor = getDescriptor(propertyName); @@ -389,7 +389,7 @@ public class BeanBinder extends Binder { * Binds {@code property} with {@code propertyType} to the field in the * {@code objectWithMemberFields} instance using {@code memberField} as a * reference to a member. - * + * * @param objectWithMemberFields * the object that contains (Java) member fields to build and * bind @@ -448,7 +448,7 @@ public class BeanBinder extends Binder { * class. If there is no suitable default constructor or you want to * configure the instantiated class then override this method and provide * your own implementation. - * + * * @see #bindInstanceFields(Object) * @param fieldClass * type of the field diff --git a/server/src/main/java/com/vaadin/data/validator/BeanValidator.java b/server/src/main/java/com/vaadin/data/validator/BeanValidator.java index 170f5a5f04..53886c9840 100644 --- a/server/src/main/java/com/vaadin/data/validator/BeanValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/BeanValidator.java @@ -73,7 +73,6 @@ public class BeanValidator implements Validator { private String propertyName; private Class beanType; - private Locale locale; /** * Creates a new JSR-303 {@code BeanValidator} that validates values of the @@ -89,25 +88,6 @@ public class BeanValidator implements Validator { * false */ public BeanValidator(Class beanType, String propertyName) { - this(beanType, propertyName, Locale.getDefault()); - } - - /** - * Creates a new JSR-303 {@code BeanValidator} that validates values of the - * specified property. Localizes validation messages using the given locale. - * - * @param beanType - * the bean class declaring the property, not null - * @param propertyName - * the property to validate, not null - * @param locale - * the locale to use, not null - * @throws IllegalStateException - * if {@link BeanUtil#checkBeanValidationAvailable()} returns - * false - */ - public BeanValidator(Class beanType, String propertyName, - Locale locale) { if (!BeanUtil.checkBeanValidationAvailable()) { throw new IllegalStateException("Cannot create a " + BeanValidator.class.getSimpleName() @@ -118,7 +98,6 @@ public class BeanValidator implements Validator { this.beanType = beanType; this.propertyName = propertyName; - setLocale(locale); } /** @@ -129,6 +108,12 @@ public class BeanValidator implements Validator { *

* Null values are accepted unless the property has an {@code @NotNull} * annotation or equivalent. + * + * @param value + * the input value to validate + * @param context + * the value context for validation + * @return the validation result */ @Override public Result apply(final Object value, ValueContext context) { @@ -137,20 +122,12 @@ public class BeanValidator implements Validator { BinaryOperator> accumulator = (result1, result2) -> result1.flatMap(val -> result2); + Locale locale = context.getLocale().orElse(Locale.getDefault()); - return violations.stream().map(v -> Result.error(getMessage(v))) + return violations.stream().map(v -> Result.error(getMessage(v, locale))) .reduce(Result.ok(value), accumulator); } - /** - * Returns the locale used for validation error messages. - * - * @return the locale used for error messages - */ - public Locale getLocale() { - return locale; - } - @Override public String toString() { return String.format("%s[%s.%s]", getClass().getSimpleName(), @@ -180,13 +157,17 @@ public class BeanValidator implements Validator { * Returns the interpolated error message for the given constraint violation * using the locale specified for this validator. * - * @param v + * @param violation * the constraint violation + * @param locale + * the used locale * @return the localized error message */ - protected String getMessage(ConstraintViolation v) { + protected String getMessage(ConstraintViolation violation, + Locale locale) { return getJavaxBeanValidatorFactory().getMessageInterpolator() - .interpolate(v.getMessageTemplate(), createContext(v), locale); + .interpolate(violation.getMessageTemplate(), + createContext(violation), locale); } /** @@ -201,18 +182,6 @@ public class BeanValidator implements Validator { return new ContextImpl(violation); } - /** - * Sets the locale used for validation error messages. Revalidation is not - * automatically triggered by setting the locale. - * - * @param locale - * the locale to use for error messages, not null - */ - private void setLocale(Locale locale) { - Objects.requireNonNull(locale, "locale cannot be null"); - this.locale = locale; - } - private static class LazyFactoryInitializer implements Serializable { private static final ValidatorFactory FACTORY = getFactory(); diff --git a/server/src/test/java/com/vaadin/data/validator/BeanValidatorTest.java b/server/src/test/java/com/vaadin/data/validator/BeanValidatorTest.java index 2d3db77939..fb504eea65 100644 --- a/server/src/test/java/com/vaadin/data/validator/BeanValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/validator/BeanValidatorTest.java @@ -7,6 +7,8 @@ import org.junit.Test; import com.vaadin.tests.data.bean.Address; import com.vaadin.tests.data.bean.BeanToValidate; +import com.vaadin.tests.util.MockUI; +import com.vaadin.ui.UI; public class BeanValidatorTest extends ValidatorTestBase { @@ -56,7 +58,10 @@ public class BeanValidatorTest extends ValidatorTestBase { @Test public void testInvalidDecimalsFailsInFrench() { - BeanValidator v = validator("decimals", Locale.FRENCH); + MockUI ui = new MockUI(); + ui.setLocale(Locale.FRENCH); + UI.setCurrent(ui); + BeanValidator v = validator("decimals"); assertFails("1234.567", "Valeur numérique hors limite " + "(<3 chiffres>.<2 chiffres> attendus)", v); } @@ -76,7 +81,4 @@ public class BeanValidatorTest extends ValidatorTestBase { return new BeanValidator(BeanToValidate.class, propertyName); } - private BeanValidator validator(String propertyName, Locale locale) { - return new BeanValidator(BeanToValidate.class, propertyName, locale); - } } -- 2.39.5