added validation hints support to ValidationUtils as well (SPR-7847)
This commit is contained in:
parent
1a2bbe5c4d
commit
207b2315ed
|
|
@ -718,7 +718,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
||||||
*/
|
*/
|
||||||
public void validate(Object... validationHints) {
|
public void validate(Object... validationHints) {
|
||||||
Validator validator = getValidator();
|
Validator validator = getValidator();
|
||||||
if (validator instanceof SmartValidator) {
|
if (!ObjectUtils.isEmpty(validationHints) && validator instanceof SmartValidator) {
|
||||||
((SmartValidator) validator).validate(getTarget(), getBindingResult(), validationHints);
|
((SmartValidator) validator).validate(getTarget(), getBindingResult(), validationHints);
|
||||||
}
|
}
|
||||||
else if (validator != null) {
|
else if (validator != null) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2007 the original author or authors.
|
* Copyright 2002-2011 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -20,6 +20,7 @@ import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -46,11 +47,26 @@ public abstract class ValidationUtils {
|
||||||
* @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
|
* @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
|
||||||
* @param obj the object to bind the parameters to
|
* @param obj the object to bind the parameters to
|
||||||
* @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
|
* @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
|
||||||
* @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is <code>null</code>;
|
* @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is
|
||||||
* or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
|
* <code>null</code>, or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
|
||||||
* the validation of the supplied object's type
|
* the validation of the supplied object's type
|
||||||
*/
|
*/
|
||||||
public static void invokeValidator(Validator validator, Object obj, Errors errors) {
|
public static void invokeValidator(Validator validator, Object obj, Errors errors) {
|
||||||
|
invokeValidator(validator, obj, errors, (Class[]) null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke the given {@link Validator}/{@link SmartValidator} for the supplied object and
|
||||||
|
* {@link Errors} instance.
|
||||||
|
* @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
|
||||||
|
* @param obj the object to bind the parameters to
|
||||||
|
* @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
|
||||||
|
* @param validationHints one or more hint objects to be passed to the validation engine
|
||||||
|
* @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is
|
||||||
|
* <code>null</code>, or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
|
||||||
|
* the validation of the supplied object's type
|
||||||
|
*/
|
||||||
|
public static void invokeValidator(Validator validator, Object obj, Errors errors, Object... validationHints) {
|
||||||
Assert.notNull(validator, "Validator must not be null");
|
Assert.notNull(validator, "Validator must not be null");
|
||||||
Assert.notNull(errors, "Errors object must not be null");
|
Assert.notNull(errors, "Errors object must not be null");
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
|
|
@ -60,7 +76,12 @@ public abstract class ValidationUtils {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Validator [" + validator.getClass() + "] does not support [" + obj.getClass() + "]");
|
"Validator [" + validator.getClass() + "] does not support [" + obj.getClass() + "]");
|
||||||
}
|
}
|
||||||
validator.validate(obj, errors);
|
if (!ObjectUtils.isEmpty(validationHints) && validator instanceof SmartValidator) {
|
||||||
|
((SmartValidator) validator).validate(obj, errors, validationHints);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
validator.validate(obj, errors);
|
||||||
|
}
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
if (errors.hasErrors()) {
|
if (errors.hasErrors()) {
|
||||||
logger.debug("Validator found " + errors.getErrorCount() + " errors");
|
logger.debug("Validator found " + errors.getErrorCount() + " errors");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue