Search in sources :

Example 6 with BindingResult

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

the class RelaxedDataBinderTests method testBindErrorTypeMismatch.

@Test
public void testBindErrorTypeMismatch() throws Exception {
    VanillaTarget target = new VanillaTarget();
    BindingResult result = bind(target, "foo: bar\n" + "value: foo");
    assertThat(result.getErrorCount()).isEqualTo(1);
}
Also used : BindingResult(org.springframework.validation.BindingResult) Test(org.junit.Test)

Example 7 with BindingResult

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

the class RelaxedDataBinderTests method testAllowedFields.

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

Example 8 with BindingResult

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

the class RelaxedDataBinderTests method testBindMapWithDeepClashInProperties.

@Test
public void testBindMapWithDeepClashInProperties() throws Exception {
    Map<String, Object> target = new LinkedHashMap<>();
    BindingResult result = bind(target, "vanilla.spam.foo: bar\n" + "vanilla.spam.foo.value: 123", "vanilla");
    assertThat(result.getErrorCount()).isEqualTo(0);
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) target.get("spam");
    assertThat(map.get("foo.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)

Example 9 with BindingResult

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

the class RelaxedDataBinderTests method doTestBindCaseInsensitiveEnums.

private void doTestBindCaseInsensitiveEnums(VanillaTarget target) throws Exception {
    BindingResult result = bind(target, "bingo: THIS");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getBingo()).isEqualTo(Bingo.THIS);
    result = bind(target, "bingo: oR");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getBingo()).isEqualTo(Bingo.or);
    result = bind(target, "bingo: that");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getBingo()).isEqualTo(Bingo.THAT);
    result = bind(target, "bingo: the-other");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getBingo()).isEqualTo(Bingo.THE_OTHER);
    result = bind(target, "bingo: the_other");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getBingo()).isEqualTo(Bingo.THE_OTHER);
    result = bind(target, "bingo: The_Other");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getBingo()).isEqualTo(Bingo.THE_OTHER);
    result = bind(target, "bingos: The_Other");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getBingos()).contains(Bingo.THE_OTHER);
    result = bind(target, "bingos: The_Other, that");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getBingos()).contains(Bingo.THE_OTHER, Bingo.THAT);
}
Also used : BindingResult(org.springframework.validation.BindingResult)

Example 10 with BindingResult

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

the class RelaxedDataBinderTests method testNoNestedFields.

@Test
public void testNoNestedFields() throws Exception {
    VanillaTarget target = new VanillaTarget();
    RelaxedDataBinder binder = getBinder(target, "foo");
    binder.setIgnoreUnknownFields(false);
    binder.setIgnoreNestedProperties(true);
    BindingResult result = bind(binder, target, "foo.foo: bar\n" + "foo.value: 123\n" + "foo.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)

Aggregations

BindingResult (org.springframework.validation.BindingResult)140 Test (org.junit.Test)28 Test (org.junit.jupiter.api.Test)27 List (java.util.List)23 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)23 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 ResponseEntity (org.springframework.http.ResponseEntity)20 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)20 Collectors (java.util.stream.Collectors)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 FieldError (org.springframework.validation.FieldError)16 ObjectError (org.springframework.validation.ObjectError)16 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