Here are the examples of the java api org.springframework.validation.Errors.rejectValue() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
263 Examples
19
View Source File : HasFieldErrorsTest.java
License : Apache License 2.0
Project Creator : isstac
License : Apache License 2.0
Project Creator : isstac
@Test
public void shouldMatchIfFieldAndCodeAreNonNullAndContainedInGivenErrorsWithMultipleFieldErrors() {
matcher = HasFieldErrors.hasFieldErrors("uuid", "invalid.uuid");
item.rejectValue("uuid", "duplicate.uuid");
item.rejectValue("uuid", "invalid.uuid");
replacedertTrue(matcher.matchesSafely(item));
}
19
View Source File : HasFieldErrorsTest.java
License : Apache License 2.0
Project Creator : isstac
License : Apache License 2.0
Project Creator : isstac
@Test
public void shouldNotMatchIfFieldAndCodeAreNonNullAndGivenErrorsDoesNotContainSpecificErrorCode() {
matcher = HasFieldErrors.hasFieldErrors("uuid", "error.null");
item.rejectValue("uuid", "duplicate.uuid");
item.rejectValue("uuid", "invalid.uuid");
replacedertFalse(matcher.matchesSafely(item));
}
19
View Source File : HasFieldErrorsTest.java
License : Apache License 2.0
Project Creator : isstac
License : Apache License 2.0
Project Creator : isstac
@Test
public void shouldNotMatchIfFieldIsNonNullButNotContainedInGivenErrors() {
matcher = HasFieldErrors.hasFieldErrors("text");
item.rejectValue("uuid", "duplicate.uuid");
replacedertFalse(matcher.matchesSafely(item));
}
19
View Source File : HasFieldErrorsTest.java
License : Apache License 2.0
Project Creator : isstac
License : Apache License 2.0
Project Creator : isstac
@Test
public void shouldMatchIfFieldIsNonNullAndContainedInGivenErrors() {
matcher = HasFieldErrors.hasFieldErrors("uuid");
item.rejectValue("uuid", "duplicate.uuid");
item.rejectValue("text", "error.null");
replacedertTrue(matcher.matchesSafely(item));
}
19
View Source File : HasFieldErrorsTest.java
License : Apache License 2.0
Project Creator : isstac
License : Apache License 2.0
Project Creator : isstac
@Test
public void shouldNotMatchIfFieldAndCodeAreNonNullAndGivenErrorsDoesNotContainSpecificField() {
matcher = HasFieldErrors.hasFieldErrors("id", "error.null");
item.rejectValue("text", "error.null");
item.rejectValue("uuid", "invalid.uuid");
replacedertFalse(matcher.matchesSafely(item));
}
19
View Source File : HasFieldErrorsTest.java
License : Apache License 2.0
Project Creator : isstac
License : Apache License 2.0
Project Creator : isstac
@Test
public void shouldMatchIfFieldAndCodeAreNullAndGivenErrorHasFieldErrors() {
matcher = HasFieldErrors.hasFieldErrors();
item.rejectValue("text", "error.null");
replacedertTrue(matcher.matchesSafely(item));
}
19
View Source File : HasFieldErrorsTest.java
License : Apache License 2.0
Project Creator : isstac
License : Apache License 2.0
Project Creator : isstac
@Test
public void shouldMatchIfFieldAndCodeAreNonNullAndContainedInGivenErrorsWithASingleFieldError() {
matcher = HasFieldErrors.hasFieldErrors("uuid", "duplicate.uuid");
item.rejectValue("uuid", "duplicate.uuid");
replacedertTrue(matcher.matchesSafely(item));
}
18
View Source File : SamplePropertiesValidator.java
License : Apache License 2.0
Project Creator : yuanmabiji
License : Apache License 2.0
Project Creator : yuanmabiji
@Override
public void validate(Object o, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "host", "host.empty");
ValidationUtils.rejectIfEmpty(errors, "port", "port.empty");
SampleProperties properties = (SampleProperties) o;
if (properties.getHost() != null && !this.pattern.matcher(properties.getHost()).matches()) {
errors.rejectValue("host", "Invalid host");
}
}
18
View Source File : CiTypeAttrValidator.java
License : Apache License 2.0
Project Creator : WeBankPartners
License : Apache License 2.0
Project Creator : WeBankPartners
private void validateBooleanField(String filedName, Integer val, Errors errors) {
BooleanType type = BooleanType.fromCode(val);
if (BooleanType.None.equals(type)) {
errors.rejectValue(filedName, "Invalid display type value");
}
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
this.tag.doEndTag();
String output = getOutput();
replacedertTrue(output.contains("id=\"testBean.errors\""));
replacedertTrue(output.contains("object error"));
replacedertTrue(output.contains("field error"));
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
replacedertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
String bodyContent = "Foo";
this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
this.tag.doEndTag();
this.tag.doFinally();
replacedertEquals(bodyContent, getOutput());
replacedertNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* https://jira.spring.io/browse/SPR-2788
*/
@Test
public void asBodyTagWithErrorsAndExistingMessagesAttributeInNonPageScopeAreNotClobbered() throws Exception {
String existingAttribute = "something";
getPageContext().setAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, existingAttribute, PageContext.APPLICATION_SCOPE);
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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
replacedertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
replacedertTrue(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List);
String bodyContent = "Foo";
this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
this.tag.doEndTag();
this.tag.doFinally();
replacedertEquals(bodyContent, getOutput());
replacedertEquals(existingAttribute, getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, PageContext.APPLICATION_SCOPE));
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
replacedertEquals(Tag.EVAL_PAGE, result);
replacedertEquals(mockContent, getOutput());
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void withErrorsAndDynamicAttributes() throws Exception {
String dynamicAttribute1 = "attr1";
String dynamicAttribute2 = "attr2";
this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1);
this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2);
// 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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
replacedertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1);
replacedertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2);
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default Message");
replacedertBlockTagContains(output, "Too Short");
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* 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();
replacedertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
this.tag.doEndTag();
String output = getOutput();
replacedertTrue(output.contains("id=\"testBean.errors\""));
replacedertTrue(output.contains("object error"));
replacedertFalse(output.contains("field error"));
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
replacedertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default <> Message");
replacedertBlockTagContains(output, "Too & Short");
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
replacedertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default <> Message");
replacedertBlockTagContains(output, "Too & Short");
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void withErrorsAndCustomElement() 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);
this.tag.setElement("div");
int result = this.tag.doStartTag();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
replacedertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default Message");
replacedertBlockTagContains(output, "Too Short");
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
replacedertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "Default Message");
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
replacedertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
replacedertTrue(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List);
String bodyContent = "Foo";
this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
this.tag.doEndTag();
this.tag.doFinally();
replacedertEquals(bodyContent, getOutput());
replacedertEquals(existingAttribute, getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
replacedertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default Message");
replacedertBlockTagContains(output, "Too Short");
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
replacedertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "Default Message");
}
18
View Source File : ErrorsTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
this.tag.doEndTag();
String output = getOutput();
replacedertTrue(output.contains("id=\"name.errors\""));
replacedertFalse(output.contains("object error"));
replacedertTrue(output.contains("field error"));
}
18
View Source File : RemoteCatalogRequestValidator.java
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Unidata
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Unidata
public void validate(Object obj, Errors errs) {
RemoteCatalogRequest rcr = (RemoteCatalogRequest) obj;
try {
URI catUri = new URI(rcr.getCatalog());
if (!catUri.isAbsolute())
errs.rejectValue("catalogUri", "catalogUri.notAbsolute", "The catalog parameter must be an absolute URI.");
if (catUri.getScheme() != null && !catUri.getScheme().equalsIgnoreCase("HTTP") && !catUri.getScheme().equalsIgnoreCase("HTTPS"))
errs.rejectValue("catalogUri", "catalogUri.notHttpUri", "The \"catalogUri\" field must be an HTTP|HTTPS URI.");
rcr.setCatalogUri(catUri);
} catch (URISyntaxException e) {
errs.rejectValue("catalog", "catalogUri.notAbsolute", "catalog parameter is not a valid URI");
}
if (rcr.getDataset() != null)
rcr.setCommand(RemoteCatalogRequest.Command.SUBSET);
else if (rcr.getCommand() == null)
rcr.setCommand(RemoteCatalogRequest.Command.SHOW);
}
18
View Source File : CourseValidator.java
License : The Unlicense
Project Creator : suriarasai
License : The Unlicense
Project Creator : suriarasai
@Override
public void validate(Object arg0, Errors arg1) {
Course course = (Course) arg0;
if ((course.getFromDate() != null && course.getToDate() != null) && (course.getFromDate().compareTo(course.getToDate()) > 0)) {
arg1.reject("toDate", "End date should be greater than start date.");
arg1.rejectValue("toDate", "error.dates", "to date must be > from date");
}
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "courseName", "error.courseName", "Course name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "fromDate", "error.fromDate", "From Date is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "toDate", "error.toDate", "To Date is required.");
}
18
View Source File : PetValidator.java
License : Apache License 2.0
Project Creator : spring-projects-experimental
License : Apache License 2.0
Project Creator : spring-projects-experimental
@Override
public void validate(Object obj, Errors errors) {
Pet pet = (Pet) obj;
String name = pet.getName();
// name validation
if (!StringUtils.hasLength(name)) {
errors.rejectValue("name", REQUIRED, REQUIRED);
}
// type validation
if (pet.isNew() && pet.getType() == null) {
errors.rejectValue("type", REQUIRED, REQUIRED);
}
// birth date validation
if (pet.getBirthDate() == null) {
errors.rejectValue("birthDate", REQUIRED, REQUIRED);
}
}
18
View Source File : DateHasTimeValidator.java
License : Apache License 2.0
Project Creator : spot-next
License : Apache License 2.0
Project Creator : spot-next
/**
* {@inheritDoc}
*/
@Override
public void validate(final Object date, final Errors errors) {
if (!isValid((Date) date))
errors.rejectValue(message, "notset");
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void withErrorsAndCustomElement() 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);
this.tag.setElement("div");
int result = this.tag.doStartTag();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
replacedertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default Message");
replacedertBlockTagContains(output, "Too Short");
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
replacedertThat(result).isEqualTo(Tag.EVAL_PAGE);
replacedertThat(getOutput()).isEqualTo(mockContent);
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* 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();
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isNotNull();
this.tag.doEndTag();
String output = getOutput();
replacedertThat(output.contains("id=\"testBean.errors\"")).isTrue();
replacedertThat(output.contains("object error")).isTrue();
replacedertThat(output.contains("field error")).isFalse();
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
replacedertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "Default Message");
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isNotNull();
this.tag.doEndTag();
String output = getOutput();
replacedertThat(output.contains("id=\"name.errors\"")).isTrue();
replacedertThat(output.contains("object error")).isFalse();
replacedertThat(output.contains("field error")).isTrue();
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isNotNull();
boolean condition = getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List;
replacedertThat(condition).isTrue();
String bodyContent = "Foo";
this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
this.tag.doEndTag();
this.tag.doFinally();
replacedertThat(getOutput()).isEqualTo(bodyContent);
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isEqualTo(existingAttribute);
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isNotNull();
String bodyContent = "Foo";
this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
this.tag.doEndTag();
this.tag.doFinally();
replacedertThat(getOutput()).isEqualTo(bodyContent);
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isNull();
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
replacedertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default <> Message");
replacedertBlockTagContains(output, "Too & Short");
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isNotNull();
this.tag.doEndTag();
String output = getOutput();
replacedertThat(output.contains("id=\"testBean.errors\"")).isTrue();
replacedertThat(output.contains("object error")).isTrue();
replacedertThat(output.contains("field error")).isTrue();
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
replacedertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default <> Message");
replacedertBlockTagContains(output, "Too & Short");
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
replacedertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "Default Message");
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* https://jira.spring.io/browse/SPR-2788
*/
@Test
public void asBodyTagWithErrorsAndExistingMessagesAttributeInNonPageScopeAreNotClobbered() throws Exception {
String existingAttribute = "something";
getPageContext().setAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, existingAttribute, PageContext.APPLICATION_SCOPE);
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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isNotNull();
boolean condition = getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List;
replacedertThat(condition).isTrue();
String bodyContent = "Foo";
this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
this.tag.doEndTag();
this.tag.doFinally();
replacedertThat(getOutput()).isEqualTo(bodyContent);
replacedertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, PageContext.APPLICATION_SCOPE)).isEqualTo(existingAttribute);
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
replacedertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default Message");
replacedertBlockTagContains(output, "Too Short");
}
18
View Source File : ErrorsTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void withErrorsAndDynamicAttributes() throws Exception {
String dynamicAttribute1 = "attr1";
String dynamicAttribute2 = "attr2";
this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1);
this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2);
// 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();
replacedertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
replacedertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
replacedertElementTagOpened(output);
replacedertElementTagClosed(output);
replacedertContainsAttribute(output, "id", "name.errors");
replacedertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1);
replacedertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2);
replacedertBlockTagContains(output, "<br/>");
replacedertBlockTagContains(output, "Default Message");
replacedertBlockTagContains(output, "Too Short");
}
18
View Source File : SettingsFormValidator.java
License : GNU Affero General Public License v3.0
Project Creator : retro-game
License : GNU Affero General Public License v3.0
Project Creator : retro-game
@Override
public void validate(Object o, Errors errors) {
SettingsForm form = (SettingsForm) o;
if (!languages.contains(form.getLanguage())) {
errors.rejectValue("language", "invalidLanguage");
}
if (!skins.contains(form.getSkin())) {
errors.rejectValue("skin", "invalidSkin");
}
}
18
View Source File : ReviewFormValidator.java
License : MIT License
Project Creator : pzinsta
License : MIT License
Project Creator : pzinsta
@Override
public void validate(Object target, Errors errors) {
ReviewForm reviewForm = (ReviewForm) target;
boolean validImages = reviewForm.getImages().stream().allMatch(ReviewFormValidator::isValidImage);
if (!validImages) {
errors.rejectValue("images", "image.format");
}
}
18
View Source File : PizzaBuilderFormValidator.java
License : MIT License
Project Creator : pzinsta
License : MIT License
Project Creator : pzinsta
private void validateCrustId(PizzaBuilderForm pizzaBuilderForm, Errors errors) {
boolean validCrustId = orderService.getCrusts().stream().map(Crust::getId).anyMatch(pizzaBuilderForm.getCrustId()::equals);
if (!validCrustId) {
errors.rejectValue("crustId", "crustId.invalid", ArrayUtils.toArray(pizzaBuilderForm.getCrustId()), "crustId.invalid");
}
}
18
View Source File : PizzaBuilderFormValidator.java
License : MIT License
Project Creator : pzinsta
License : MIT License
Project Creator : pzinsta
private void validateQuanreplacedy(PizzaBuilderForm pizzaBuilderForm, Errors errors) {
if (!Range.closed(pizzaQuanreplacedyMin, pizzaQuanreplacedyMax).contains(pizzaBuilderForm.getQuanreplacedy())) {
errors.rejectValue("quanreplacedy", "pizza.quanreplacedy.out.of.range", ArrayUtils.toArray(pizzaQuanreplacedyMin, pizzaQuanreplacedyMax), "pizza.quanreplacedy.out.of.range");
}
}
18
View Source File : PizzaBuilderFormValidator.java
License : MIT License
Project Creator : pzinsta
License : MIT License
Project Creator : pzinsta
private void validateCutStyleId(PizzaBuilderForm pizzaBuilderForm, Errors errors) {
boolean validCutStyleId = orderService.getCutStyles().stream().map(CutStyle::getId).anyMatch(pizzaBuilderForm.getCutStyleId()::equals);
if (!validCutStyleId) {
errors.rejectValue("cutStyleId", "cutStyleId.invalid", ArrayUtils.toArray(pizzaBuilderForm.getCutStyleId()), "cutStyleId.invalid");
}
}
18
View Source File : PizzaBuilderFormValidator.java
License : MIT License
Project Creator : pzinsta
License : MIT License
Project Creator : pzinsta
private void validatePizzaSizeId(PizzaBuilderForm pizzaBuilderForm, Errors errors) {
boolean validPizzaSizeId = orderService.getPizzaSizes().stream().map(PizzaSize::getId).anyMatch(pizzaBuilderForm.getPizzaSizeId()::equals);
if (!validPizzaSizeId) {
errors.rejectValue("pizzaSizeId", "pizzaSizeId.invalid", ArrayUtils.toArray(pizzaBuilderForm.getPizzaSizeId()), "pizzaSizeId.invalid");
}
}
18
View Source File : PizzaBuilderFormValidator.java
License : MIT License
Project Creator : pzinsta
License : MIT License
Project Creator : pzinsta
private void validateBakeStyleId(PizzaBuilderForm pizzaBuilderForm, Errors errors) {
boolean validBakeStyleId = orderService.getBakeStyles().stream().map(BakeStyle::getId).anyMatch(pizzaBuilderForm.getBakeStyleId()::equals);
if (!validBakeStyleId) {
errors.rejectValue("bakeStyleId", "bakeStyleId.invalid", ArrayUtils.toArray(pizzaBuilderForm.getBakeStyleId()), "bakeStyleId.invalid");
}
}
18
View Source File : DeliveryAddressValidator.java
License : MIT License
Project Creator : pzinsta
License : MIT License
Project Creator : pzinsta
public void validateEnterGuestDeliveryAddress(DeliveryAddress deliveryAddress, Errors errors) {
if (!cities.contains(deliveryAddress.getCity())) {
errors.rejectValue("city", "city.invalid");
}
}
18
View Source File : CustomerValidator.java
License : MIT License
Project Creator : pzinsta
License : MIT License
Project Creator : pzinsta
public void validateEnterGuestInformation(Customer customer, Errors errors) {
if (StringUtils.isEmpty(customer.getFirstName())) {
errors.rejectValue("firstName", "firstName.blank");
}
}
See More Examples