Skip to content

Commit 696dcb7

Browse files
committed
SpringValidatorAdapter allows for custom field name resolution
Issue: SPR-14104
1 parent 739ae8c commit 696dcb7

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ protected String determineErrorCode(ConstraintDescriptor<?> descriptor) {
191191
* Return FieldError arguments for a validation error on the given field.
192192
* Invoked for each violated constraint.
193193
* <p>The default implementation returns a first argument indicating the field name
194-
* (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes).
195-
* Afterwards, it adds all actual constraint annotation attributes (i.e. excluding
196-
* "message", "groups" and "payload") in alphabetical order of their attribute names.
194+
* (see {@link #getResolvableField}). Afterwards, it adds all actual constraint
195+
* annotation attributes (i.e. excluding "message", "groups" and "payload") in
196+
* alphabetical order of their attribute names.
197197
* <p>Can be overridden to e.g. add further attributes from the constraint descriptor.
198198
* @param objectName the name of the target object
199199
* @param field the field that caused the binding error
@@ -205,8 +205,7 @@ protected String determineErrorCode(ConstraintDescriptor<?> descriptor) {
205205
*/
206206
protected Object[] getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor<?> descriptor) {
207207
List<Object> arguments = new LinkedList<Object>();
208-
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
209-
arguments.add(new DefaultMessageSourceResolvable(codes, field));
208+
arguments.add(getResolvableField(objectName, field));
210209
// Using a TreeMap for alphabetical ordering of attribute names
211210
Map<String, Object> attributesToExpose = new TreeMap<String, Object>();
212211
for (Map.Entry<String, Object> entry : descriptor.getAttributes().entrySet()) {
@@ -223,6 +222,22 @@ protected Object[] getArgumentsForConstraint(String objectName, String field, Co
223222
return arguments.toArray(new Object[arguments.size()]);
224223
}
225224

225+
/**
226+
* Build a resolvable wrapper for the specified field, allowing to resolve the field's
227+
* name in a {@code MessageSource}.
228+
* <p>The default implementation returns a first argument indicating the field:
229+
* of type {@code DefaultMessageSourceResolvable}, with "objectName.field" and "field"
230+
* as codes, and with the plain field name as default message.
231+
* @param objectName the name of the target object
232+
* @param field the field that caused the binding error
233+
* @return a corresponding {@code MessageSourceResolvable} for the specified field
234+
* @since 4.3
235+
*/
236+
protected MessageSourceResolvable getResolvableField(String objectName, String field) {
237+
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
238+
return new DefaultMessageSourceResolvable(codes, field);
239+
}
240+
226241
/**
227242
* Extract the rejected value behind the given constraint violation,
228243
* for exposure through the Spring errors representation.

0 commit comments

Comments
 (0)