Skip to content

Commit 0543a29

Browse files
committed
expose arrays as comma-delimited element String
1 parent 7840683 commit 0543a29

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

org.springframework.context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import org.springframework.beans.PropertyAccessException;
2020
import org.springframework.context.support.DefaultMessageSourceResolvable;
21+
import org.springframework.util.ObjectUtils;
22+
import org.springframework.util.StringUtils;
2123

2224
/**
2325
* Default {@link BindingErrorProcessor} implementation.
@@ -65,8 +67,12 @@ public void processPropertyAccessException(PropertyAccessException ex, BindingRe
6567
String field = ex.getPropertyName();
6668
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
6769
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
70+
Object rejectedValue = ex.getValue();
71+
if (rejectedValue != null && rejectedValue.getClass().isArray()) {
72+
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
73+
}
6874
bindingResult.addError(new FieldError(
69-
bindingResult.getObjectName(), field, ex.getValue(), true,
75+
bindingResult.getObjectName(), field, rejectedValue, true,
7076
codes, arguments, ex.getLocalizedMessage()));
7177
}
7278

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@
1616

1717
package org.springframework.web.servlet.mvc.annotation;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
23-
import static org.junit.Assert.assertSame;
24-
import static org.junit.Assert.assertTrue;
25-
import static org.junit.Assert.fail;
26-
2719
import java.io.IOException;
2820
import java.io.Serializable;
2921
import java.io.Writer;
@@ -43,7 +35,6 @@
4335
import java.util.Locale;
4436
import java.util.Map;
4537
import java.util.Set;
46-
4738
import javax.servlet.ServletConfig;
4839
import javax.servlet.ServletContext;
4940
import javax.servlet.ServletException;
@@ -54,8 +45,9 @@
5445
import javax.validation.Valid;
5546
import javax.validation.constraints.NotNull;
5647

57-
import org.junit.Ignore;
48+
import static org.junit.Assert.*;
5849
import org.junit.Test;
50+
5951
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
6052
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
6153
import org.springframework.aop.support.DefaultPointcutAdvisor;
@@ -90,6 +82,7 @@
9082
import org.springframework.util.StringUtils;
9183
import org.springframework.validation.BindingResult;
9284
import org.springframework.validation.Errors;
85+
import org.springframework.validation.FieldError;
9386
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
9487
import org.springframework.web.bind.WebDataBinder;
9588
import org.springframework.web.bind.annotation.CookieValue;
@@ -1303,6 +1296,9 @@ public List<TestBean> getTestBeans() {
13031296

13041297
@RequestMapping("/myPath.do")
13051298
public String myHandle(@ModelAttribute("myCommand") TestBean tb, BindingResult errors, ModelMap model) {
1299+
FieldError error = errors.getFieldError("age");
1300+
assertNotNull("Must have field error for age property", error);
1301+
assertEquals("value2", error.getRejectedValue());
13061302
if (!model.containsKey("myKey")) {
13071303
model.addAttribute("myKey", "myValue");
13081304
}

0 commit comments

Comments
 (0)