JSR-303 SpringValidatorAdapter uses field name as first argument (analogous to bind errors; SPR-6407)
This commit is contained in:
parent
5165465821
commit
69124f9392
|
|
@ -78,9 +78,10 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return FieldError arguments for a binding error on the given field.
|
* Return FieldError arguments for a binding error on the given field.
|
||||||
* Invoked for each missing required fields and each type mismatch.
|
* Invoked for each missing required field and each type mismatch.
|
||||||
* <p>Default implementation returns a DefaultMessageSourceResolvable
|
* <p>The default implementation returns a single argument of type
|
||||||
* with "objectName.field" and "field" as codes.
|
* DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes.
|
||||||
|
* @param objectName the name of the target object
|
||||||
* @param field the field that caused the binding error
|
* @param field the field that caused the binding error
|
||||||
* @return the Object array that represents the FieldError arguments
|
* @return the Object array that represents the FieldError arguments
|
||||||
* @see org.springframework.validation.FieldError#getArguments
|
* @see org.springframework.validation.FieldError#getArguments
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,14 @@
|
||||||
|
|
||||||
package org.springframework.validation.beanvalidation;
|
package org.springframework.validation.beanvalidation;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.validation.ConstraintViolation;
|
import javax.validation.ConstraintViolation;
|
||||||
import javax.validation.metadata.BeanDescriptor;
|
import javax.validation.metadata.BeanDescriptor;
|
||||||
|
import javax.validation.metadata.ConstraintDescriptor;
|
||||||
|
|
||||||
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
|
|
@ -74,12 +78,33 @@ public class SpringValidatorAdapter implements Validator, javax.validation.Valid
|
||||||
if (fieldError == null || !fieldError.isBindingFailure()) {
|
if (fieldError == null || !fieldError.isBindingFailure()) {
|
||||||
errors.rejectValue(field,
|
errors.rejectValue(field,
|
||||||
violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(),
|
violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(),
|
||||||
violation.getConstraintDescriptor().getAttributes().values().toArray(),
|
getArgumentsForConstraint(errors.getObjectName(), field, violation.getConstraintDescriptor()),
|
||||||
violation.getMessage());
|
violation.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return FieldError arguments for a validation error on the given field.
|
||||||
|
* Invoked for each violated constraint.
|
||||||
|
* <p>The default implementation returns a single argument of type
|
||||||
|
* DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes.
|
||||||
|
* @param objectName the name of the target object
|
||||||
|
* @param field the field that caused the binding error
|
||||||
|
* @param descriptor the JSR-303 constraint descriptor
|
||||||
|
* @return the Object array that represents the FieldError arguments
|
||||||
|
* @see org.springframework.validation.FieldError#getArguments
|
||||||
|
* @see org.springframework.context.support.DefaultMessageSourceResolvable
|
||||||
|
* @see org.springframework.validation.DefaultBindingErrorProcessor#getArgumentsForBindError
|
||||||
|
*/
|
||||||
|
protected Object[] getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor<?> descriptor) {
|
||||||
|
List<Object> arguments = new LinkedList<Object>();
|
||||||
|
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
|
||||||
|
arguments.add(new DefaultMessageSourceResolvable(codes, field));
|
||||||
|
arguments.addAll(descriptor.getAttributes().values());
|
||||||
|
return arguments.toArray(new Object[arguments.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Implementation of JSR-303 Validator interface
|
// Implementation of JSR-303 Validator interface
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue