@ -16,10 +16,14 @@
@@ -16,10 +16,14 @@
package org.springframework.validation.beanvalidation ;
import java.util.LinkedList ;
import java.util.List ;
import java.util.Set ;
import javax.validation.ConstraintViolation ;
import javax.validation.metadata.BeanDescriptor ;
import javax.validation.metadata.ConstraintDescriptor ;
import org.springframework.context.support.DefaultMessageSourceResolvable ;
import org.springframework.util.Assert ;
import org.springframework.validation.Errors ;
import org.springframework.validation.FieldError ;
@ -74,12 +78,33 @@ public class SpringValidatorAdapter implements Validator, javax.validation.Valid
@@ -74,12 +78,33 @@ public class SpringValidatorAdapter implements Validator, javax.validation.Valid
if ( fieldError = = null | | ! fieldError . isBindingFailure ( ) ) {
errors . rejectValue ( field ,
violation . getConstraintDescriptor ( ) . getAnnotation ( ) . annotationType ( ) . getSimpleName ( ) ,
violation . getConstraintDescriptor ( ) . getAttributes ( ) . values ( ) . toArray ( ) ,
getArgumentsForConstraint ( errors . getObjectName ( ) , field , violation . getConstraintDescriptor ( ) ) ,
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