added validation hints support to ValidationUtils as well (SPR-7847)

master
Juergen Hoeller 13 years ago
parent 1a2bbe5c4d
commit 207b2315ed
  1. 2
      org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java
  2. 27
      org.springframework.context/src/main/java/org/springframework/validation/ValidationUtils.java

@ -718,7 +718,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
*/
public void validate(Object... validationHints) {
Validator validator = getValidator();
if (validator instanceof SmartValidator) {
if (!ObjectUtils.isEmpty(validationHints) && validator instanceof SmartValidator) {
((SmartValidator) validator).validate(getTarget(), getBindingResult(), validationHints);
}
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");
* 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.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
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 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>)
* @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}
* @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) {
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(errors, "Errors object must not be null");
if (logger.isDebugEnabled()) {
@ -60,7 +76,12 @@ public abstract class ValidationUtils {
throw new IllegalArgumentException(
"Validator [" + validator.getClass() + "] does not support [" + obj.getClass() + "]");
}
if (!ObjectUtils.isEmpty(validationHints) && validator instanceof SmartValidator) {
((SmartValidator) validator).validate(obj, errors, validationHints);
}
else {
validator.validate(obj, errors);
}
if (logger.isDebugEnabled()) {
if (errors.hasErrors()) {
logger.debug("Validator found " + errors.getErrorCount() + " errors");

Loading…
Cancel
Save