Class SpringValidatorAdapter
- All Implemented Interfaces:
- Validator,- SmartValidator,- Validator
- Direct Known Subclasses:
- CustomValidatorBean,- LocalValidatorFactoryBean
javax.validator.Validator and
 exposes it as a Spring Validator
 while also exposing the original JSR-303 Validator interface itself.
 Can be used as a programmatic wrapper. Also serves as base class for
 CustomValidatorBean and LocalValidatorFactoryBean,
 and as the primary implementation of the SmartValidator interface.
 
This adapter is fully compatible with Bean Validation 1.1 as well as 2.0.
- Since:
- 3.0
- Author:
- Juergen Hoeller, Sam Brannen
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionSpringValidatorAdapter(Validator targetValidator) Create a new SpringValidatorAdapter for the given JSR-303 Validator.
- 
Method SummaryModifier and TypeMethodDescriptionprotected StringdetermineErrorCode(ConstraintDescriptor<?> descriptor) Determine a Spring-reported error code for the given constraint descriptor.protected StringdetermineField(ConstraintViolation<Object> violation) Determine a field for the given constraint violation.protected Object[]getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor<?> descriptor) Return FieldError arguments for a validation error on the given field.getConstraintsForClass(Class<?> clazz) protected ObjectgetRejectedValue(String field, ConstraintViolation<Object> violation, BindingResult bindingResult) Extract the rejected value behind the given constraint violation, for exposure through the Spring errors representation.protected MessageSourceResolvablegetResolvableField(String objectName, String field) Build a resolvable wrapper for the specified field, allowing to resolve the field's name in aMessageSource.protected voidprocessConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) Process the given JSR-303 ConstraintViolations, adding corresponding errors to the provided SpringErrorsobject.protected booleanrequiresMessageFormat(ConstraintViolation<?> violation) Indicate whether this violation's interpolated message has remaining placeholders and therefore requiresMessageFormatto be applied to it.boolean<T> TReturn a contained validator instance of the specified type, unwrapping as far as necessary.voidValidate the giventargetobject which must be of aClassfor which theValidator.supports(Class)method typically has returned (or would return)true.voidValidate the suppliedtargetobject, which must be of a type ofClassfor which theValidator.supports(Class)method typically returnstrue.<T> Set<ConstraintViolation<T>><T> Set<ConstraintViolation<T>>validateProperty(T object, String propertyName, Class<?>... groups) voidvalidateValue(Class<?> targetType, String fieldName, Object value, Errors errors, Object... validationHints) Validate the supplied value for the specified field on the target type, reporting the same validation errors as if the value would be bound to the field on an instance of the target class.<T> Set<ConstraintViolation<T>>validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.validation.ValidatorvalidateObject
- 
Constructor Details- 
SpringValidatorAdapterCreate a new SpringValidatorAdapter for the given JSR-303 Validator.- Parameters:
- targetValidator- the JSR-303 Validator to wrap
 
 
- 
- 
Method Details- 
supportsDescription copied from interface:Validator
- 
validateDescription copied from interface:ValidatorValidate the giventargetobject which must be of aClassfor which theValidator.supports(Class)method typically has returned (or would return)true.The supplied errorsinstance can be used to report any resulting validation errors, typically as part of a larger binding process which this validator is meant to participate in. Binding errors have typically been pre-registered with theerrorsinstance before this invocation already.
- 
validateDescription copied from interface:SmartValidatorValidate the suppliedtargetobject, which must be of a type ofClassfor which theValidator.supports(Class)method typically returnstrue.The supplied errorsinstance can be used to report any resulting validation errors.This variant of validate()supports validation hints, such as validation groups against a JSR-303 provider (in which case, the provided hint objects need to be annotation arguments of typeClass).Note: Validation hints may get ignored by the actual target Validator, in which case this method should behave just like its regularValidator.validate(Object, Errors)sibling.- Specified by:
- validatein interface- SmartValidator
- Parameters:
- target- the object that is to be validated
- errors- contextual state about the validation process
- validationHints- one or more hint objects to be passed to the validation engine
- See Also:
 
- 
validateValuepublic void validateValue(Class<?> targetType, String fieldName, @Nullable Object value, Errors errors, Object... validationHints) Description copied from interface:SmartValidatorValidate the supplied value for the specified field on the target type, reporting the same validation errors as if the value would be bound to the field on an instance of the target class.- Specified by:
- validateValuein interface- SmartValidator
- Parameters:
- targetType- the target type
- fieldName- the name of the field
- value- the candidate value
- errors- contextual state about the validation process
- validationHints- one or more hint objects to be passed to the validation engine
- See Also:
 
- 
processConstraintViolationsprotected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) Process the given JSR-303 ConstraintViolations, adding corresponding errors to the provided SpringErrorsobject.- Parameters:
- violations- the JSR-303 ConstraintViolation results
- errors- the Spring errors object to register to
 
- 
determineFieldDetermine a field for the given constraint violation.The default implementation returns the stringified property path. - Parameters:
- violation- the current JSR-303 ConstraintViolation
- Returns:
- the Spring-reported field (for use with Errors)
- Since:
- 4.2
- See Also:
 
- 
determineErrorCodeDetermine a Spring-reported error code for the given constraint descriptor.The default implementation returns the simple class name of the descriptor's annotation type. Note that the configured MessageCodesResolverwill automatically generate error code variations which include the object name and the field name.- Parameters:
- descriptor- the JSR-303 ConstraintDescriptor for the current violation
- Returns:
- a corresponding error code (for use with Errors)
- Since:
- 4.2
- See Also:
 
- 
getArgumentsForConstraintprotected Object[] getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor<?> descriptor) 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 (see getResolvableField(java.lang.String, java.lang.String)). 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, for example, add further attributes from the constraint descriptor. - Parameters:
- objectName- the name of the target object
- field- the field that caused the binding error
- descriptor- the JSR-303 constraint descriptor
- Returns:
- the Object array that represents the FieldError arguments
- See Also:
 
- 
getResolvableFieldBuild a resolvable wrapper for the specified field, allowing to resolve the field's name in aMessageSource.The default implementation returns a first argument indicating the field: of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes, and with the plain field name as default message.- Parameters:
- objectName- the name of the target object
- field- the field that caused the binding error
- Returns:
- a corresponding MessageSourceResolvablefor the specified field
- Since:
- 4.3
- See Also:
 
- 
getRejectedValue@Nullable protected Object getRejectedValue(String field, ConstraintViolation<Object> violation, BindingResult bindingResult) Extract the rejected value behind the given constraint violation, for exposure through the Spring errors representation.- Parameters:
- field- the field that caused the binding error
- violation- the corresponding JSR-303 ConstraintViolation
- bindingResult- a Spring BindingResult for the backing object which contains the current field's value
- Returns:
- the invalid value to expose as part of the field error
- Since:
- 4.2
- See Also:
 
- 
requiresMessageFormatIndicate whether this violation's interpolated message has remaining placeholders and therefore requiresMessageFormatto be applied to it. Called for a Bean Validation defined message (coming outValidationMessages.properties) when rendered as the default message in Spring's MessageSource.The default implementation considers a Spring-style "{0}" placeholder for the field name as an indication for MessageFormat. Any other placeholder or escape syntax occurrences are typically a mismatch, coming out of regex pattern values or the like. Note that standard Bean Validation does not support "{0}" style placeholders at all; this is a feature typically used in Spring MessageSource resource bundles.- Parameters:
- violation- the Bean Validation constraint violation, including BV-defined interpolation of named attribute references in its message
- Returns:
- trueif- java.text.MessageFormatis to be applied, or- falseif the violation's message should be used as-is
- Since:
- 5.1.8
- See Also:
 
- 
validate
- 
validatePropertypublic <T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups) - Specified by:
- validatePropertyin interface- Validator
 
- 
validateValuepublic <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) - Specified by:
- validateValuein interface- Validator
 
- 
getConstraintsForClass- Specified by:
- getConstraintsForClassin interface- Validator
 
- 
unwrapDescription copied from interface:SmartValidatorReturn a contained validator instance of the specified type, unwrapping as far as necessary.- Specified by:
- unwrapin interface- SmartValidator
- Specified by:
- unwrapin interface- Validator
- Type Parameters:
- T- the type of the object to return
- Parameters:
- type- the class of the object to return
- Returns:
- a validator instance of the specified type; nullif there isn't a nested validator; an exception may be raised if the specified validator type does not match.
 
- 
forExecutables- Specified by:
- forExecutablesin interface- Validator
 
 
-