Object/FieldError provides access to source object (exception/violation)
Issue: SPR-16372
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -59,9 +59,9 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
|
||||
String fixedField = bindingResult.getNestedPath() + missingField;
|
||||
String[] codes = bindingResult.resolveMessageCodes(MISSING_FIELD_ERROR_CODE, missingField);
|
||||
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), fixedField);
|
||||
bindingResult.addError(new FieldError(
|
||||
bindingResult.getObjectName(), fixedField, "", true,
|
||||
codes, arguments, "Field '" + fixedField + "' is required"));
|
||||
FieldError error = new FieldError(bindingResult.getObjectName(), fixedField, "", true,
|
||||
codes, arguments, "Field '" + fixedField + "' is required");
|
||||
bindingResult.addError(error);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -72,12 +72,13 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
|
||||
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
|
||||
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
|
||||
Object rejectedValue = ex.getValue();
|
||||
if (rejectedValue != null && rejectedValue.getClass().isArray()) {
|
||||
if (ObjectUtils.isArray(rejectedValue)) {
|
||||
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
|
||||
}
|
||||
bindingResult.addError(new FieldError(
|
||||
bindingResult.getObjectName(), field, rejectedValue, true,
|
||||
codes, arguments, ex.getLocalizedMessage()));
|
||||
FieldError error = new FieldError(bindingResult.getObjectName(), field, rejectedValue, true,
|
||||
codes, arguments, ex.getLocalizedMessage());
|
||||
error.initSource(ex);
|
||||
bindingResult.addError(error);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -99,13 +99,6 @@ public class FieldError extends ObjectError {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Field error in object '" + getObjectName() + "' on field '" + this.field +
|
||||
"': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " +
|
||||
resolvableToString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
if (this == other) {
|
||||
@@ -129,4 +122,11 @@ public class FieldError extends ObjectError {
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Field error in object '" + getObjectName() + "' on field '" + this.field +
|
||||
"': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " +
|
||||
resolvableToString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -37,6 +37,9 @@ public class ObjectError extends DefaultMessageSourceResolvable {
|
||||
|
||||
private final String objectName;
|
||||
|
||||
@Nullable
|
||||
private Object source;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance of the ObjectError class.
|
||||
@@ -70,12 +73,31 @@ public class ObjectError extends DefaultMessageSourceResolvable {
|
||||
return this.objectName;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Error in object '" + this.objectName + "': " + resolvableToString();
|
||||
/**
|
||||
* Initialize the source behind this error: possibly an {@link Exception}
|
||||
* (typically {@link org.springframework.beans.PropertyAccessException})
|
||||
* or a Bean Validation {@link javax.validation.ConstraintViolation}.
|
||||
* @param source the source object
|
||||
* @since 5.0.4
|
||||
*/
|
||||
public void initSource(Object source) {
|
||||
Assert.state(this.source == null, "Source already initialized");
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the source behind this error: possibly an {@link Exception}
|
||||
* (typically {@link org.springframework.beans.PropertyAccessException})
|
||||
* or a Bean Validation {@link javax.validation.ConstraintViolation}.
|
||||
* @return the source object, or {@code null} if none
|
||||
* @since 5.0.4
|
||||
*/
|
||||
@Nullable
|
||||
public Object getSource() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
if (this == other) {
|
||||
@@ -93,4 +115,9 @@ public class ObjectError extends DefaultMessageSourceResolvable {
|
||||
return super.hashCode() * 29 + getObjectName().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Error in object '" + this.objectName + "': " + resolvableToString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -142,15 +142,18 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
|
||||
String nestedField = bindingResult.getNestedPath() + field;
|
||||
if ("".equals(nestedField)) {
|
||||
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
|
||||
bindingResult.addError(new ObjectError(
|
||||
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage()));
|
||||
ObjectError error = new ObjectError(
|
||||
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage());
|
||||
error.initSource(violation);
|
||||
bindingResult.addError(error);
|
||||
}
|
||||
else {
|
||||
Object rejectedValue = getRejectedValue(field, violation, bindingResult);
|
||||
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
|
||||
bindingResult.addError(new FieldError(
|
||||
errors.getObjectName(), nestedField, rejectedValue, false,
|
||||
errorCodes, errorArgs, violation.getMessage()));
|
||||
FieldError error = new FieldError(errors.getObjectName(), nestedField,
|
||||
rejectedValue, false, errorCodes, errorArgs, violation.getMessage());
|
||||
error.initSource(violation);
|
||||
bindingResult.addError(error);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user