|
16 | 16 |
|
17 | 17 | package org.springframework.validation.beanvalidation;
|
18 | 18 |
|
| 19 | +import java.util.LinkedList; |
| 20 | +import java.util.List; |
19 | 21 | import java.util.Set;
|
20 | 22 | import javax.validation.ConstraintViolation;
|
21 | 23 | import javax.validation.metadata.BeanDescriptor;
|
| 24 | +import javax.validation.metadata.ConstraintDescriptor; |
22 | 25 |
|
| 26 | +import org.springframework.context.support.DefaultMessageSourceResolvable; |
23 | 27 | import org.springframework.util.Assert;
|
24 | 28 | import org.springframework.validation.Errors;
|
25 | 29 | import org.springframework.validation.FieldError;
|
@@ -74,12 +78,33 @@ public void validate(Object target, Errors errors) {
|
74 | 78 | if (fieldError == null || !fieldError.isBindingFailure()) {
|
75 | 79 | errors.rejectValue(field,
|
76 | 80 | violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(),
|
77 |
| - violation.getConstraintDescriptor().getAttributes().values().toArray(), |
| 81 | + getArgumentsForConstraint(errors.getObjectName(), field, violation.getConstraintDescriptor()), |
78 | 82 | violation.getMessage());
|
79 | 83 | }
|
80 | 84 | }
|
81 | 85 | }
|
82 | 86 |
|
| 87 | + /** |
| 88 | + * Return FieldError arguments for a validation error on the given field. |
| 89 | + * Invoked for each violated constraint. |
| 90 | + * <p>The default implementation returns a single argument of type |
| 91 | + * DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes. |
| 92 | + * @param objectName the name of the target object |
| 93 | + * @param field the field that caused the binding error |
| 94 | + * @param descriptor the JSR-303 constraint descriptor |
| 95 | + * @return the Object array that represents the FieldError arguments |
| 96 | + * @see org.springframework.validation.FieldError#getArguments |
| 97 | + * @see org.springframework.context.support.DefaultMessageSourceResolvable |
| 98 | + * @see org.springframework.validation.DefaultBindingErrorProcessor#getArgumentsForBindError |
| 99 | + */ |
| 100 | + protected Object[] getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor<?> descriptor) { |
| 101 | + List<Object> arguments = new LinkedList<Object>(); |
| 102 | + String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; |
| 103 | + arguments.add(new DefaultMessageSourceResolvable(codes, field)); |
| 104 | + arguments.addAll(descriptor.getAttributes().values()); |
| 105 | + return arguments.toArray(new Object[arguments.size()]); |
| 106 | + } |
| 107 | + |
83 | 108 |
|
84 | 109 | //---------------------------------------------------------------------
|
85 | 110 | // Implementation of JSR-303 Validator interface
|
|
0 commit comments