From 207b2315eda924916e12cfc97c652d63a04c8053 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 3 Dec 2011 15:54:52 +0000 Subject: [PATCH] added validation hints support to ValidationUtils as well (SPR-7847) --- .../validation/DataBinder.java | 2 +- .../validation/ValidationUtils.java | 29 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java b/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java index 448da57812..7758b7f4c9 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.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) { diff --git a/org.springframework.context/src/main/java/org/springframework/validation/ValidationUtils.java b/org.springframework.context/src/main/java/org/springframework/validation/ValidationUtils.java index 63e4a99860..e7867a7633 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/ValidationUtils.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/ValidationUtils.java @@ -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 Validator to be invoked (must not be null) * @param obj the object to bind the parameters to * @param errors the {@link Errors} instance that should store the errors (must not be null) - * @throws IllegalArgumentException if either of the Validator or Errors arguments is null; - * or if the supplied Validator does not {@link Validator#supports(Class) support} + * @throws IllegalArgumentException if either of the Validator or Errors arguments is + * null, or if the supplied Validator 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 Validator to be invoked (must not be null) + * @param obj the object to bind the parameters to + * @param errors the {@link Errors} instance that should store the errors (must not be null) + * @param validationHints one or more hint objects to be passed to the validation engine + * @throws IllegalArgumentException if either of the Validator or Errors arguments is + * null, or if the supplied Validator 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() + "]"); } - 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 (errors.hasErrors()) { logger.debug("Validator found " + errors.getErrorCount() + " errors");