diff --git a/org.springframework.context/src/main/java/org/springframework/validation/AbstractBindingResult.java b/org.springframework.context/src/main/java/org/springframework/validation/AbstractBindingResult.java index 1cf6526656..7525a3dede 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/AbstractBindingResult.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/AbstractBindingResult.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -120,13 +120,6 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi this.errors.addAll(errors.getAllErrors()); } - /** - * Resolve the given error code into message codes. - * Calls the MessageCodesResolver with appropriate parameters. - * @param errorCode the error code to resolve into message codes - * @return the resolved message codes - * @see #setMessageCodesResolver - */ public String[] resolveMessageCodes(String errorCode) { return getMessageCodesResolver().resolveMessageCodes(errorCode, getObjectName()); } diff --git a/org.springframework.context/src/main/java/org/springframework/validation/BindException.java b/org.springframework.context/src/main/java/org/springframework/validation/BindException.java index 8c401edaa1..e7896996b3 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/BindException.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/BindException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2012 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. @@ -220,6 +220,10 @@ public class BindException extends Exception implements BindingResult { this.bindingResult.addError(error); } + public String[] resolveMessageCodes(String errorCode) { + return this.bindingResult.resolveMessageCodes(errorCode); + } + public String[] resolveMessageCodes(String errorCode, String field) { return this.bindingResult.resolveMessageCodes(errorCode, field); } diff --git a/org.springframework.context/src/main/java/org/springframework/validation/BindingResult.java b/org.springframework.context/src/main/java/org/springframework/validation/BindingResult.java index 848c3e0292..3069a42a17 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/BindingResult.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/BindingResult.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 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. @@ -112,6 +112,14 @@ public interface BindingResult extends Errors { */ void addError(ObjectError error); + /** + * Resolve the given error code into message codes. + *
Calls the configured {@link MessageCodesResolver} with appropriate parameters. + * @param errorCode the error code to resolve into message codes + * @return the resolved message codes + */ + String[] resolveMessageCodes(String errorCode); + /** * Resolve the given error code into message codes for the given field. *
Calls the configured {@link MessageCodesResolver} with appropriate parameters.
diff --git a/org.springframework.context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java b/org.springframework.context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java
index 91fd75ab60..82f1d91390 100644
--- a/org.springframework.context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java
+++ b/org.springframework.context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2011 the original author or authors.
+ * Copyright 2002-2012 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.
@@ -119,12 +119,11 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
// can do custom FieldError registration with invalid value from ConstraintViolation,
// as necessary for Hibernate Validator compatibility (non-indexed set path in field)
BindingResult bindingResult = (BindingResult) errors;
- String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
String nestedField = bindingResult.getNestedPath() + field;
- ObjectError error;
if ("".equals(nestedField)) {
- error = new ObjectError(
- errors.getObjectName(), errorCodes, errorArgs, violation.getMessage());
+ String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
+ bindingResult.addError(new ObjectError(
+ errors.getObjectName(), errorCodes, errorArgs, violation.getMessage()));
}
else {
Object invalidValue = violation.getInvalidValue();
@@ -132,11 +131,11 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
// bean constraint with property path: retrieve the actual property value
invalidValue = bindingResult.getRawFieldValue(field);
}
- error = new FieldError(
+ String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
+ bindingResult.addError(new FieldError(
errors.getObjectName(), nestedField, invalidValue, false,
- errorCodes, errorArgs, violation.getMessage());
+ errorCodes, errorArgs, violation.getMessage()));
}
- bindingResult.addError(error);
}
else {
// got no BindingResult - can only do standard rejectValue call
diff --git a/org.springframework.context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java b/org.springframework.context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java
index 4e69535108..dbfe0b7d1a 100644
--- a/org.springframework.context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java
+++ b/org.springframework.context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2010 the original author or authors.
+ * Copyright 2002-2012 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.
@@ -110,11 +110,22 @@ public class ValidatorFactoryTests {
assertEquals(2, result.getErrorCount());
FieldError fieldError = result.getFieldError("name");
assertEquals("name", fieldError.getField());
- System.out.println(Arrays.asList(fieldError.getCodes()));
+ List