Search in sources :

Example 76 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testOnlyTopLevelFields.

@Test
public void testOnlyTopLevelFields() throws Exception {
    VanillaTarget target = new VanillaTarget();
    RelaxedDataBinder binder = getBinder(target, null);
    binder.setIgnoreUnknownFields(false);
    binder.setIgnoreNestedProperties(true);
    BindingResult result = bind(binder, target, "foo: bar\n" + "value: 123\n" + "nested.bar: spam");
    assertThat(target.getValue()).isEqualTo(123);
    assertThat(target.getFoo()).isEqualTo("bar");
    assertThat(result.getErrorCount()).isEqualTo(0);
}
Also used : BindingResult(org.springframework.validation.BindingResult) Test(org.junit.Test)

Example 77 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindOverlappingNestedMaps.

@SuppressWarnings("unchecked")
@Test
public void testBindOverlappingNestedMaps() throws Exception {
    Map<String, Object> target = new LinkedHashMap<>();
    BindingResult result = bind(target, "a.b.c.d: abc\na.b.c1.d1: efg");
    assertThat(result.getErrorCount()).isEqualTo(0);
    Map<String, Object> a = (Map<String, Object>) target.get("a");
    Map<String, Object> b = (Map<String, Object>) a.get("b");
    Map<String, Object> c = (Map<String, Object>) b.get("c");
    assertThat(c.get("d")).isEqualTo("abc");
    Map<String, Object> c1 = (Map<String, Object>) b.get("c1");
    assertThat(c1.get("d1")).isEqualTo("efg");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 78 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testSimpleValidation.

@Test
public void testSimpleValidation() throws Exception {
    ValidatedTarget target = new ValidatedTarget();
    BindingResult result = bind(target, "");
    assertThat(result.getErrorCount()).isEqualTo(1);
}
Also used : BindingResult(org.springframework.validation.BindingResult) Test(org.junit.Test)

Example 79 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testRequiredFieldsValidation.

@Test
public void testRequiredFieldsValidation() throws Exception {
    TargetWithValidatedMap target = new TargetWithValidatedMap();
    BindingResult result = bind(target, "info[foo]: bar");
    assertThat(result.getErrorCount()).isEqualTo(2);
    for (FieldError error : result.getFieldErrors()) {
        System.err.println(new StaticMessageSource().getMessage(error, Locale.getDefault()));
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) StaticMessageSource(org.springframework.context.support.StaticMessageSource) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 80 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindMapWithDifferentDeepClashInProperties.

@Test
public void testBindMapWithDifferentDeepClashInProperties() throws Exception {
    Map<String, Object> target = new LinkedHashMap<>();
    BindingResult result = bind(target, "vanilla.spam.bar: bar\n" + "vanilla.spam.bar.value: 123", "vanilla");
    assertThat(result.getErrorCount()).isEqualTo(0);
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) target.get("spam");
    assertThat(map.get("bar.value")).isEqualTo("123");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

BindingResult (org.springframework.validation.BindingResult)143 Test (org.junit.jupiter.api.Test)29 Test (org.junit.Test)28 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)24 List (java.util.List)23 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)21 ResponseEntity (org.springframework.http.ResponseEntity)20 Collectors (java.util.stream.Collectors)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 FieldError (org.springframework.validation.FieldError)18 ObjectError (org.springframework.validation.ObjectError)17 Model (org.springframework.ui.Model)15 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)14 HashMap (java.util.HashMap)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 DefaultProblem (org.zalando.problem.DefaultProblem)13 Problem (org.zalando.problem.Problem)13