From 696dcb72a5089642367ddf73fb34a231a9abd68a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 1 Apr 2016 10:29:56 +0200 Subject: [PATCH] SpringValidatorAdapter allows for custom field name resolution Issue: SPR-14104 --- .../SpringValidatorAdapter.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java index 9934c104237..6d46113d831 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java @@ -191,9 +191,9 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation. * Return FieldError arguments for a validation error on the given field. * Invoked for each violated constraint. *

The default implementation returns a first argument indicating the field name - * (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes). - * Afterwards, it adds all actual constraint annotation attributes (i.e. excluding - * "message", "groups" and "payload") in alphabetical order of their attribute names. + * (see {@link #getResolvableField}). Afterwards, it adds all actual constraint + * annotation attributes (i.e. excluding "message", "groups" and "payload") in + * alphabetical order of their attribute names. *

Can be overridden to e.g. add further attributes from the constraint descriptor. * @param objectName the name of the target object * @param field the field that caused the binding error @@ -205,8 +205,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation. */ protected Object[] getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor descriptor) { List arguments = new LinkedList(); - String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; - arguments.add(new DefaultMessageSourceResolvable(codes, field)); + arguments.add(getResolvableField(objectName, field)); // Using a TreeMap for alphabetical ordering of attribute names Map attributesToExpose = new TreeMap(); for (Map.Entry entry : descriptor.getAttributes().entrySet()) { @@ -223,6 +222,22 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation. return arguments.toArray(new Object[arguments.size()]); } + /** + * Build a resolvable wrapper for the specified field, allowing to resolve the field's + * name in a {@code MessageSource}. + *

The default implementation returns a first argument indicating the field: + * of type {@code DefaultMessageSourceResolvable}, with "objectName.field" and "field" + * as codes, and with the plain field name as default message. + * @param objectName the name of the target object + * @param field the field that caused the binding error + * @return a corresponding {@code MessageSourceResolvable} for the specified field + * @since 4.3 + */ + protected MessageSourceResolvable getResolvableField(String objectName, String field) { + String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; + return new DefaultMessageSourceResolvable(codes, field); + } + /** * Extract the rejected value behind the given constraint violation, * for exposure through the Spring errors representation.