fixed SpringValidatorAdapter regression to return correct error codes for class-level constraints (SPR-8958)
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
* <p>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.
|
||||
* <p>Calls the configured {@link MessageCodesResolver} with appropriate parameters.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user