@Override public void rejectValue(@Nullable String field, String errorCode) { this.source.rejectValue(field, errorCode); }
@Override public void rejectValue(@Nullable String field, String errorCode, String defaultMessage) { this.source.rejectValue(field, errorCode, defaultMessage); }
@Override public void rejectValue(@Nullable String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage) { this.source.rejectValue(field, errorCode, errorArgs, defaultMessage); }
public class UserValidator implements Validator { @Override public boolean supports(Class clazz) { return User.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { User user = (User) target; if(user.getName() == null) { errors.rejectValue("name", "your_error_code"); } // do "complex" validation here } }
@Override public void validate(@Nullable Object obj, Errors errors) { TestBean tb = (TestBean) obj; if (tb == null || "XXX".equals(tb.getName())) { errors.rejectValue("", "SPOUSE_NOT_AVAILABLE"); return; } if (tb.getAge() < 32) { errors.rejectValue("age", "TOO_YOUNG", "simply too young"); } } }
@Override public void validate(@Nullable Object target, Errors errors) { TestBean testBean = (TestBean) target; if (testBean.getFoo() == null) { errors.rejectValue("foo", "nullValue"); } } }
@Override public void validate(@Nullable Object obj, Errors errors) { TestBean tb = (TestBean) obj; if (tb.getAge() < 32) { errors.rejectValue("age", "TOO_YOUNG", "simply too young"); } if (tb.getAge() % 2 == 0) { errors.rejectValue("age", "AGE_NOT_ODD", "your age isn't odd"); } if (tb.getName() == null || !tb.getName().equals("Rod")) { errors.rejectValue("name", "NOT_ROD", "are you sure you're not Rod?"); } if (tb.getTouchy() == null || !tb.getTouchy().equals(tb.getName())) { errors.reject("NAME_TOUCHY_MISMATCH", "name and touchy do not match"); } if (tb.getAge() == 0) { errors.reject("GENERAL_ERROR", new String[] {"arg"}, "msg"); } } }
@Test public void specificPathMatchesSpecificFieldOnly() throws Exception { this.tag.setPath("name"); Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.reject("some.code", "object error"); errors.rejectValue("name", "some.code", "field error"); exposeBindingResult(errors); this.tag.doStartTag(); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); this.tag.doEndTag(); String output = getOutput(); assertTrue(output.contains("id=\"name.errors\"")); assertFalse(output.contains("object error")); assertTrue(output.contains("field error")); }
@Test public void starMatchesAllErrors() throws Exception { this.tag.setPath("*"); Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.reject("some.code", "object error"); errors.rejectValue("name", "some.code", "field error"); exposeBindingResult(errors); this.tag.doStartTag(); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); this.tag.doEndTag(); String output = getOutput(); assertTrue(output.contains("id=\"testBean.errors\"")); assertTrue(output.contains("object error")); assertTrue(output.contains("field error")); }
/** * https://jira.spring.io/browse/SPR-4005 */ @Test public void omittedPathMatchesObjectErrorsOnly() throws Exception { this.tag.setPath(null); Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.reject("some.code", "object error"); errors.rejectValue("name", "some.code", "field error"); exposeBindingResult(errors); this.tag.doStartTag(); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); this.tag.doEndTag(); String output = getOutput(); assertTrue(output.contains("id=\"testBean.errors\"")); assertTrue(output.contains("object error")); assertFalse(output.contains("field error")); }
@Test public void propertyExposing() throws JspException { PageContext pc = createPageContext(); TestBean tb = new TestBean(); tb.setName("name1"); Errors errors = new BindException(tb, "tb"); errors.rejectValue("name", "code1", null, "message & 1"); errors.rejectValue("name", "code2", null, "message2"); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); // test global property (should be null) BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); assertNull(tag.getProperty()); // test property set (tb.name) tag.release(); tag.setPageContext(pc); tag.setPath("tb.name"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); assertEquals("name", tag.getProperty()); }
@Test public void withErrors() throws Exception { // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default Message"); errors.rejectValue("name", "too.short", "Too Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertBlockTagContains(output, "<br/>"); assertBlockTagContains(output, "Default Message"); assertBlockTagContains(output, "Too Short"); }
@Test public void withEscapedErrors() throws Exception { // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default <> Message"); errors.rejectValue("name", "too.short", "Too & Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertBlockTagContains(output, "<br/>"); assertBlockTagContains(output, "Default <> Message"); assertBlockTagContains(output, "Too & Short"); }
@Test public void withNonEscapedErrors() throws Exception { this.tag.setHtmlEscape(false); // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default <> Message"); errors.rejectValue("name", "too.short", "Too & Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertBlockTagContains(output, "<br/>"); assertBlockTagContains(output, "Default <> Message"); assertBlockTagContains(output, "Too & Short"); }
@Test public void asBodyTag() throws Exception { Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.rejectValue("name", "some.code", "Default Message"); errors.rejectValue("name", "too.short", "Too Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); String bodyContent = "Foo"; this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); this.tag.doEndTag(); this.tag.doFinally(); assertEquals(bodyContent, getOutput()); assertNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); }
@Test public void asBodyTagWithExistingMessagesAttribute() throws Exception { String existingAttribute = "something"; getPageContext().setAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, existingAttribute); Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.rejectValue("name", "some.code", "Default Message"); errors.rejectValue("name", "too.short", "Too Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); assertTrue(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List); String bodyContent = "Foo"; this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); this.tag.doEndTag(); this.tag.doFinally(); assertEquals(bodyContent, getOutput()); assertEquals(existingAttribute, getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); }
@Test public void withExplicitNonWhitespaceBodyContent() throws Exception { String mockContent = "This is some explicit body content"; this.tag.setBodyContent(new MockBodyContent(mockContent, getWriter())); // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default Message"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); assertEquals(mockContent, getOutput()); }
@Test public void testAddAllErrors() { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "32x"); binder.bind(pvs); Errors errors = binder.getBindingResult(); BeanPropertyBindingResult errors2 = new BeanPropertyBindingResult(rod, "person"); errors.rejectValue("name", "badName"); errors.addAllErrors(errors2); FieldError ageError = errors.getFieldError("age"); assertEquals("typeMismatch", ageError.getCode()); FieldError nameError = errors.getFieldError("name"); assertEquals("badName", nameError.getCode()); }
@Test public void withExplicitEmptyWhitespaceBodyContent() throws Exception { this.tag.setBodyContent(new MockBodyContent("", getWriter())); // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default Message"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertBlockTagContains(output, "Default Message"); }
@Test public void withExplicitWhitespaceBodyContent() throws Exception { this.tag.setBodyContent(new MockBodyContent("\t\n ", getWriter())); // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default Message"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertBlockTagContains(output, "Default Message"); }