Skip to content

Commit 207b231

Browse files
committed
added validation hints support to ValidationUtils as well (SPR-7847)
1 parent 1a2bbe5 commit 207b231

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ public void validate() {
718718
*/
719719
public void validate(Object... validationHints) {
720720
Validator validator = getValidator();
721-
if (validator instanceof SmartValidator) {
721+
if (!ObjectUtils.isEmpty(validationHints) && validator instanceof SmartValidator) {
722722
((SmartValidator) validator).validate(getTarget(), getBindingResult(), validationHints);
723723
}
724724
else if (validator != null) {

org.springframework.context/src/main/java/org/springframework/validation/ValidationUtils.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import org.apache.commons.logging.LogFactory;
2121

2222
import org.springframework.util.Assert;
23+
import org.springframework.util.ObjectUtils;
2324
import org.springframework.util.StringUtils;
2425

2526
/**
@@ -46,11 +47,26 @@ public abstract class ValidationUtils {
4647
* @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
4748
* @param obj the object to bind the parameters to
4849
* @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
49-
* @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is <code>null</code>;
50-
* or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
50+
* @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is
51+
* <code>null</code>, or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
5152
* the validation of the supplied object's type
5253
*/
5354
public static void invokeValidator(Validator validator, Object obj, Errors errors) {
55+
invokeValidator(validator, obj, errors, (Class[]) null);
56+
}
57+
58+
/**
59+
* Invoke the given {@link Validator}/{@link SmartValidator} for the supplied object and
60+
* {@link Errors} instance.
61+
* @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
62+
* @param obj the object to bind the parameters to
63+
* @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
64+
* @param validationHints one or more hint objects to be passed to the validation engine
65+
* @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is
66+
* <code>null</code>, or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
67+
* the validation of the supplied object's type
68+
*/
69+
public static void invokeValidator(Validator validator, Object obj, Errors errors, Object... validationHints) {
5470
Assert.notNull(validator, "Validator must not be null");
5571
Assert.notNull(errors, "Errors object must not be null");
5672
if (logger.isDebugEnabled()) {
@@ -60,7 +76,12 @@ public static void invokeValidator(Validator validator, Object obj, Errors error
6076
throw new IllegalArgumentException(
6177
"Validator [" + validator.getClass() + "] does not support [" + obj.getClass() + "]");
6278
}
63-
validator.validate(obj, errors);
79+
if (!ObjectUtils.isEmpty(validationHints) && validator instanceof SmartValidator) {
80+
((SmartValidator) validator).validate(obj, errors, validationHints);
81+
}
82+
else {
83+
validator.validate(obj, errors);
84+
}
6485
if (logger.isDebugEnabled()) {
6586
if (errors.hasErrors()) {
6687
logger.debug("Validator found " + errors.getErrorCount() + " errors");

0 commit comments

Comments
 (0)