@Override public boolean hasFieldErrors(String field) { return this.bindingResult.hasFieldErrors(field); }
@Override public boolean hasFieldErrors(String field) { return this.bindingResult.hasFieldErrors(field); }
@Override public boolean hasFieldErrors() { return this.bindingResult.hasFieldErrors(); }
@Override public boolean hasFieldErrors() { return this.bindingResult.hasFieldErrors(); }
@Override public boolean hasFieldErrors() { return this.bindingResult.hasFieldErrors(); }
@Override public boolean hasFieldErrors(String field) { return this.bindingResult.hasFieldErrors(field); }
@Override @RequestMapping("/myPath.do") public String myHandle(@ModelAttribute("myCommand") @Valid TestBean tb, BindingResult errors, ModelMap model) { if (!errors.hasFieldErrors("validCountry")) { throw new IllegalStateException("Declarative validation not applied"); } return super.myHandle(tb, errors, model); }
@Override @RequestMapping("/myPath.do") public String myHandle(@ModelAttribute("myCommand") @Valid TestBean tb, BindingResult errors, ModelMap model) { if (!errors.hasFieldErrors("sex")) { throw new IllegalStateException("requiredFields not applied"); } return super.myHandle(tb, errors, model); } }
/** * Assert the given model attribute field(s) have errors. */ public ResultMatcher attributeHasFieldErrors(final String name, final String... fieldNames) { return mvcResult -> { ModelAndView mav = getModelAndView(mvcResult); BindingResult result = getBindingResult(mav, name); assertTrue("No errors for attribute '" + name + "'", result.hasErrors()); for (final String fieldName : fieldNames) { boolean hasFieldErrors = result.hasFieldErrors(fieldName); assertTrue("No errors for field '" + fieldName + "' of attribute '" + name + "'", hasFieldErrors); } }; }
assertTrue("Correct number of errors", br.getErrorCount() == 1); assertTrue("Has age errors", br.hasFieldErrors("age")); assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1); assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
@Test public void testBindingErrorWithFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1x2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(0.0), tb.getMyFloat()); assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat")); assertTrue(binder.getBindingResult().hasFieldErrors("myFloat")); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test public void testBindingErrorWithFormatterAgainstList() { BeanWithIntegerList tb = new BeanWithIntegerList(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("integerList[0]", "1x2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertTrue(tb.getIntegerList().isEmpty()); assertEquals("1x2", binder.getBindingResult().getFieldValue("integerList[0]")); assertTrue(binder.getBindingResult().hasFieldErrors("integerList[0]")); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test public void testBindingErrorWithCustomFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); binder.addCustomFormatter(new NumberStyleFormatter()); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1x2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(0.0), tb.getMyFloat()); assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat")); assertTrue(binder.getBindingResult().hasFieldErrors("myFloat")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("myFloat").getCode()); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test public void testBindingErrorWithFormatterAgainstFields() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); binder.initDirectFieldAccess(); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1x2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(0.0), tb.getMyFloat()); assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat")); assertTrue(binder.getBindingResult().hasFieldErrors("myFloat")); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test public void testBindingErrorWithParseExceptionFromCustomFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); binder.addCustomFormatter(new Formatter<String>() { @Override public String parse(String text, Locale locale) throws ParseException { throw new ParseException(text, 0); } @Override public String print(String object, Locale locale) { return object; } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "test"); binder.bind(pvs); assertTrue(binder.getBindingResult().hasFieldErrors("name")); assertEquals("test", binder.getBindingResult().getFieldValue("name")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); }
@Test public void testBindingErrorWithRuntimeExceptionFromCustomFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); binder.addCustomFormatter(new Formatter<String>() { @Override public String parse(String text, Locale locale) throws ParseException { throw new RuntimeException(text); } @Override public String print(String object, Locale locale) { return object; } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "test"); binder.bind(pvs); assertTrue(binder.getBindingResult().hasFieldErrors("name")); assertEquals("test", binder.getBindingResult().getFieldValue("name")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); }
@Test public void testBindingErrorWithRuntimeExceptionFromFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatter(new Formatter<String>() { @Override public String parse(String text, Locale locale) throws ParseException { throw new RuntimeException(text); } @Override public String print(String object, Locale locale) { return object; } }); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "test"); binder.bind(pvs); assertTrue(binder.getBindingResult().hasFieldErrors("name")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); assertEquals("test", binder.getBindingResult().getFieldValue("name")); }
@Test public void testBindingErrorWithParseExceptionFromFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatter(new Formatter<String>() { @Override public String parse(String text, Locale locale) throws ParseException { throw new ParseException(text, 0); } @Override public String print(String object, Locale locale) { return object; } }); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "test"); binder.bind(pvs); assertTrue(binder.getBindingResult().hasFieldErrors("name")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); assertEquals("test", binder.getBindingResult().getFieldValue("name")); }
assertEquals("value", tb.getTouchy()); assertTrue(binder.getBindingResult().hasFieldErrors("spouse.*")); assertEquals(1, binder.getBindingResult().getFieldErrorCount("spouse.*")); assertEquals("spouse.name", binder.getBindingResult().getFieldError("spouse.*").getField());
assertEquals("value", tb.getTouchy()); assertTrue(binder.getBindingResult().hasFieldErrors("spouse.*")); assertEquals(1, binder.getBindingResult().getFieldErrorCount("spouse.*")); assertEquals("spouse.name", binder.getBindingResult().getFieldError("spouse.*").getField());