Java Code Examples for org.springframework.validation.BeanPropertyBindingResult#getFieldError()

The following examples show how to use org.springframework.validation.BeanPropertyBindingResult#getFieldError() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringValidatorAdapterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-15123
public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMessageFormat() {
	messageSource.setAlwaysUseMessageFormat(true);

	TestBean testBean = new TestBean();
	testBean.setEmail("[email protected]");
	testBean.setConfirmEmail("[email protected]");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("email"), is(1));
	assertThat(errors.getFieldValue("email"), is("[email protected]"));
	assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
	FieldError error1 = errors.getFieldError("email");
	FieldError error2 = errors.getFieldError("confirmEmail");
	assertNotNull(error1);
	assertNotNull(error2);
	assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value as confirmEmail"));
	assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required"));
	assertTrue(error1.contains(ConstraintViolation.class));
	assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email"));
	assertTrue(error2.contains(ConstraintViolation.class));
	assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("confirmEmail"));
}
 
Example 2
Source File: SpringValidatorAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPatternMessage() {
	TestBean testBean = new TestBean();
	testBean.setEmail("X");
	testBean.setConfirmEmail("X");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("email"), is(1));
	assertThat(errors.getFieldValue("email"), is("X"));
	FieldError error = errors.getFieldError("email");
	assertNotNull(error);
	assertThat(messageSource.getMessage(error, Locale.ENGLISH), containsString("[\\w.'-]{1,}@[\\w.'-]{1,}"));
	assertTrue(error.contains(ConstraintViolation.class));
	assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email"));
}
 
Example 3
Source File: SpringValidatorAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-15123
public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMessageFormat() {
	messageSource.setAlwaysUseMessageFormat(true);

	TestBean testBean = new TestBean();
	testBean.setEmail("[email protected]");
	testBean.setConfirmEmail("[email protected]");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("email"), is(1));
	assertThat(errors.getFieldValue("email"), is("[email protected]"));
	assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
	FieldError error1 = errors.getFieldError("email");
	FieldError error2 = errors.getFieldError("confirmEmail");
	assertNotNull(error1);
	assertNotNull(error2);
	assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value as confirmEmail"));
	assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required"));
	assertTrue(error1.contains(ConstraintViolation.class));
	assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email"));
	assertTrue(error2.contains(ConstraintViolation.class));
	assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("confirmEmail"));
}
 
Example 4
Source File: ValidatorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpringValidationWithErrorInSetElement() throws Exception {
	LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
	validator.afterPropertiesSet();

	ValidPerson person = new ValidPerson();
	person.getAddressSet().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("addressSet[].street");
	assertEquals("addressSet[].street", fieldError.getField());
}
 
Example 5
Source File: SpringValidatorAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-13406
public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName() {
	TestBean testBean = new TestBean();
	testBean.setPassword("password");
	testBean.setConfirmPassword("PASSWORD");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("password"), is(1));
	assertThat(errors.getFieldValue("password"), is("password"));
	FieldError error = errors.getFieldError("password");
	assertNotNull(error);
	assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Password must be same value as Password(Confirm)"));
	assertTrue(error.contains(ConstraintViolation.class));
	assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("password"));
}
 
Example 6
Source File: SpringValidatorAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-13406
public void testNoStringArgumentValue() {
	TestBean testBean = new TestBean();
	testBean.setPassword("pass");
	testBean.setConfirmPassword("pass");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("password"), is(1));
	assertThat(errors.getFieldValue("password"), is("pass"));
	FieldError error = errors.getFieldError("password");
	assertNotNull(error);
	assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Size of Password is must be between 8 and 128"));
	assertTrue(error.contains(ConstraintViolation.class));
	assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("password"));
}
 
Example 7
Source File: ValidatorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpringValidationWithErrorInSetElement() throws Exception {
	LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
	validator.afterPropertiesSet();
	ValidPerson person = new ValidPerson();
	person.getAddressSet().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("addressSet[].street");
	assertEquals("addressSet[].street", fieldError.getField());
}
 
Example 8
Source File: ValidatorFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSpringValidationWithErrorInSetElement() {
	LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
	validator.afterPropertiesSet();

	ValidPerson person = new ValidPerson();
	person.getAddressSet().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("addressSet[].street");
	assertEquals("addressSet[].street", fieldError.getField());
}
 
Example 9
Source File: SpringValidatorAdapterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-13406
public void testNoStringArgumentValue() {
	TestBean testBean = new TestBean();
	testBean.setPassword("pass");
	testBean.setConfirmPassword("pass");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("password"), is(1));
	assertThat(errors.getFieldValue("password"), is("pass"));
	FieldError error = errors.getFieldError("password");
	assertNotNull(error);
	assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Size of Password is must be between 8 and 128"));
	assertTrue(error.contains(ConstraintViolation.class));
	assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("password"));
}
 
Example 10
Source File: ValidatorFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSpringValidationWithErrorInListElement() {
	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());
}
 
Example 11
Source File: SpringValidatorAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPatternMessage() {
	TestBean testBean = new TestBean();
	testBean.setEmail("X");
	testBean.setConfirmEmail("X");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("email"), is(1));
	assertThat(errors.getFieldValue("email"), is("X"));
	FieldError error = errors.getFieldError("email");
	assertNotNull(error);
	assertThat(messageSource.getMessage(error, Locale.ENGLISH), containsString("[\\w.'-]{1,}@[\\w.'-]{1,}"));
	assertTrue(error.contains(ConstraintViolation.class));
	assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email"));
}
 
Example 12
Source File: SpringValidatorAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-15123
public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMessageFormat() {
	messageSource.setAlwaysUseMessageFormat(true);

	TestBean testBean = new TestBean();
	testBean.setEmail("[email protected]");
	testBean.setConfirmEmail("[email protected]");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("email"), is(1));
	assertThat(errors.getFieldValue("email"), is("[email protected]"));
	assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
	FieldError error1 = errors.getFieldError("email");
	FieldError error2 = errors.getFieldError("confirmEmail");
	assertNotNull(error1);
	assertNotNull(error2);
	assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value as confirmEmail"));
	assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required"));
	assertTrue(error1.contains(ConstraintViolation.class));
	assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email"));
	assertTrue(error2.contains(ConstraintViolation.class));
	assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("confirmEmail"));
}
 
Example 13
Source File: ValidatorFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testListValidation() {
	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]"));
}
 
Example 14
Source File: ValidatorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 15
Source File: SpringValidatorAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-13406
public void testNoStringArgumentValue() {
	TestBean testBean = new TestBean();
	testBean.setPassword("pass");
	testBean.setConfirmPassword("pass");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
	validatorAdapter.validate(testBean, errors);

	assertThat(errors.getFieldErrorCount("password"), is(1));
	assertThat(errors.getFieldValue("password"), is("pass"));
	FieldError error = errors.getFieldError("password");
	assertNotNull(error);
	assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Size of Password is must be between 8 and 128"));
	assertTrue(error.contains(ConstraintViolation.class));
	assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("password"));
}
 
Example 16
Source File: ValidatorFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testListValidation() {
	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]");
	assertNotNull(fieldError);
	assertEquals("X", fieldError.getRejectedValue());
	assertEquals("X", errors.getFieldValue("list[1]"));
}
 
Example 17
Source File: ValidatorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 18
Source File: ValidatorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSpringValidation() {
	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"));
}
 
Example 19
Source File: ValidatorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testSpringValidation() {
	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"));
}
 
Example 20
Source File: ValidatorFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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"));
}