From 3d57425dcb67bc9f403a412555afeb6ba962ff9f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 19 Jul 2023 23:08:42 +0200 Subject: [PATCH] Return rejected value from getFieldValue in case of error See gh-19877 --- .../java/org/springframework/validation/SimpleErrors.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spring-context/src/main/java/org/springframework/validation/SimpleErrors.java b/spring-context/src/main/java/org/springframework/validation/SimpleErrors.java index 8e42f76bbc..e768711e86 100644 --- a/spring-context/src/main/java/org/springframework/validation/SimpleErrors.java +++ b/spring-context/src/main/java/org/springframework/validation/SimpleErrors.java @@ -125,15 +125,22 @@ public class SimpleErrors implements Errors, Serializable { @Override @Nullable public Object getFieldValue(String field) { + FieldError fieldError = getFieldError(field); + if (fieldError != null) { + return fieldError.getRejectedValue(); + } + PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(this.target.getClass(), field); if (pd != null && pd.getReadMethod() != null) { return ReflectionUtils.invokeMethod(pd.getReadMethod(), this.target); } + Field rawField = ReflectionUtils.findField(this.target.getClass(), field); if (rawField != null) { ReflectionUtils.makeAccessible(rawField); return ReflectionUtils.getField(rawField, this.target); } + throw new IllegalArgumentException("Cannot retrieve value for field '" + field + "' - neither a getter method nor a raw field found"); }