Search in sources :

Example 36 with FieldError

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

the class SpringValidatorAdapterTests method testNoStringArgumentValue.

// SPR-13406
@Test
public void testNoStringArgumentValue() throws Exception {
    TestBean testBean = new TestBean();
    testBean.setPassword("pass");
    testBean.setConfirmPassword("pass");
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
    validatorAdapter.validate(testBean, errors);
    assertThat(errors.getFieldErrorCount("password")).isEqualTo(1);
    assertThat(errors.getFieldValue("password")).isEqualTo("pass");
    FieldError error = errors.getFieldError("password");
    assertThat(error).isNotNull();
    assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password must be between 8 and 128");
    assertThat(error.contains(ConstraintViolation.class)).isTrue();
    assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password");
    assertThat(SerializationTestUtils.serializeAndDeserialize(error.toString())).isEqualTo(error.toString());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 37 with FieldError

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

the class ValidatorFactoryTests method testSpringValidation.

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

Example 38 with FieldError

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

the class ValidatorFactoryTests method testSpringValidationWithErrorInListElement.

@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);
    assertThat(result.getErrorCount()).isEqualTo(3);
    FieldError fieldError = result.getFieldError("name");
    assertThat(fieldError.getField()).isEqualTo("name");
    fieldError = result.getFieldError("address.street");
    assertThat(fieldError.getField()).isEqualTo("address.street");
    fieldError = result.getFieldError("addressList[0].street");
    assertThat(fieldError.getField()).isEqualTo("addressList[0].street");
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 39 with FieldError

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

the class ValidatorFactoryTests method testListValidation.

@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]");
    assertThat(fieldError).isNotNull();
    assertThat(fieldError.getRejectedValue()).isEqualTo("X");
    assertThat(errors.getFieldValue("list[1]")).isEqualTo("X");
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Example 40 with FieldError

use of org.springframework.validation.FieldError in project molgenis by molgenis.

the class RepositoryConstraintViolationExceptionTest method testGetFieldError.

@Test
void testGetFieldError() {
    EntityErrors entityErrors = mock(EntityErrors.class);
    FieldError fieldError = new FieldError("name1", "field", "message1");
    when(entityErrors.getFieldError("field")).thenReturn(fieldError);
    RepositoryConstraintViolationException exception = new RepositoryConstraintViolationException(entityErrors);
    assertEquals(exception.getFieldError("field"), fieldError);
}
Also used : FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Aggregations

FieldError (org.springframework.validation.FieldError)101 Test (org.junit.jupiter.api.Test)24 ObjectError (org.springframework.validation.ObjectError)20 BindingResult (org.springframework.validation.BindingResult)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 ApiOperation (io.swagger.annotations.ApiOperation)13 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)12 Errors (org.springframework.validation.Errors)11 ArrayList (java.util.ArrayList)10 BindException (org.springframework.validation.BindException)7 Locale (java.util.Locale)6 CustomResult (com.megagao.production.ssm.domain.customize.CustomResult)5 Test (org.junit.Test)5 MapBindingResult (org.springframework.validation.MapBindingResult)5 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)4 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)4 ErrorMessage (org.springframework.cloud.servicebroker.model.error.ErrorMessage)4