Search in sources :

Example 36 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testSpringValidation.

@Test
public void testSpringValidation() throws Exception {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);
    assertEquals(2, result.getErrorCount());
    FieldError fieldError = result.getFieldError("name");
    assertEquals("name", fieldError.getField());
    List<String> errorCodes = Arrays.asList(fieldError.getCodes());
    assertEquals(4, errorCodes.size());
    assertTrue(errorCodes.contains("NotNull.person.name"));
    assertTrue(errorCodes.contains("NotNull.name"));
    assertTrue(errorCodes.contains("NotNull.java.lang.String"));
    assertTrue(errorCodes.contains("NotNull"));
    fieldError = result.getFieldError("address.street");
    assertEquals("address.street", fieldError.getField());
    errorCodes = Arrays.asList(fieldError.getCodes());
    assertEquals(5, errorCodes.size());
    assertTrue(errorCodes.contains("NotNull.person.address.street"));
    assertTrue(errorCodes.contains("NotNull.address.street"));
    assertTrue(errorCodes.contains("NotNull.street"));
    assertTrue(errorCodes.contains("NotNull.java.lang.String"));
    assertTrue(errorCodes.contains("NotNull"));
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 37 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testSpringValidationFieldType.

@Test
public void testSpringValidationFieldType() throws Exception {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    person.setName("Phil");
    person.getAddress().setStreet("Phil's Street");
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, errors);
    assertEquals(1, errors.getErrorCount());
    assertThat("Field/Value type mismatch", errors.getFieldError("address").getRejectedValue(), instanceOf(ValidAddress.class));
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Test(org.junit.Test)

Example 38 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testSpringValidationWithErrorInListElement.

@Test
public void testSpringValidationWithErrorInListElement() throws Exception {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    person.getAddressList().add(new ValidAddress());
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);
    assertEquals(3, result.getErrorCount());
    FieldError fieldError = result.getFieldError("name");
    assertEquals("name", fieldError.getField());
    fieldError = result.getFieldError("address.street");
    assertEquals("address.street", fieldError.getField());
    fieldError = result.getFieldError("addressList[0].street");
    assertEquals("addressList[0].street", fieldError.getField());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 39 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testValidationWithOptionalField.

@Test
public void testValidationWithOptionalField() throws Exception {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    MainBeanWithOptional mainBean = new MainBeanWithOptional();
    Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean");
    validator.validate(mainBean, errors);
    Object rejected = errors.getFieldValue("inner.value");
    assertNull(rejected);
}
Also used : Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Test(org.junit.Test)

Example 40 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testInnerBeanValidation.

@Test
public void testInnerBeanValidation() throws Exception {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    MainBean mainBean = new MainBean();
    Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean");
    validator.validate(mainBean, errors);
    Object rejected = errors.getFieldValue("inner.value");
    assertNull(rejected);
}
Also used : Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Test(org.junit.Test)

Aggregations

BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)73 Test (org.junit.Test)60 TestBean (org.springframework.tests.sample.beans.TestBean)19 Errors (org.springframework.validation.Errors)18 StringReader (java.io.StringReader)17 Document (org.dom4j.Document)17 Element (org.dom4j.Element)17 SAXReader (org.dom4j.io.SAXReader)17 BindingResult (org.springframework.validation.BindingResult)11 PropertyEditorSupport (java.beans.PropertyEditorSupport)10 List (java.util.List)10 ArrayList (java.util.ArrayList)8 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)6 CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)6 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)6 LinkedList (java.util.LinkedList)5 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)5 Model (org.springframework.ui.Model)5 FieldError (org.springframework.validation.FieldError)5 ObjectError (org.springframework.validation.ObjectError)5