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));
}
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());
}
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);
}
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);
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class ValidatorFactoryTests method testListValidation.
@Test
public void testListValidation() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ListContainer listContainer = new ListContainer();
listContainer.addString("A");
listContainer.addString("X");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(listContainer, "listContainer");
errors.initConversion(new DefaultConversionService());
validator.validate(listContainer, errors);
FieldError fieldError = errors.getFieldError("list[1]");
assertEquals("X", errors.getFieldValue("list[1]"));
}
Aggregations