Search in sources :

Example 71 with BindingResult

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

the class ModelResultMatchers method attributeHasFieldErrors.

/**
	 * Assert the given model attribute field(s) have errors.
	 */
public ResultMatcher attributeHasFieldErrors(final String name, final String... fieldNames) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult mvcResult) throws Exception {
            ModelAndView mav = getModelAndView(mvcResult);
            BindingResult result = getBindingResult(mav, name);
            assertTrue("No errors for attribute: [" + name + "]", result.hasErrors());
            for (final String fieldName : fieldNames) {
                boolean hasFieldErrors = result.hasFieldErrors(fieldName);
                assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors);
            }
        }
    };
}
Also used : BindingResult(org.springframework.validation.BindingResult) ModelAndView(org.springframework.web.servlet.ModelAndView) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 72 with BindingResult

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

the class BindingAwareModelMap method removeBindingResultIfNecessary.

private void removeBindingResultIfNecessary(Object key, Object value) {
    if (key instanceof String) {
        String attributeName = (String) key;
        if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
            String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName;
            BindingResult bindingResult = (BindingResult) get(bindingResultKey);
            if (bindingResult != null && bindingResult.getTarget() != value) {
                remove(bindingResultKey);
            }
        }
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult)

Example 73 with BindingResult

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

the class DefaultErrorAttributesTests method extractMethodArgumentNotValidExceptionBindingResultErrors.

@Test
public void extractMethodArgumentNotValidExceptionBindingResultErrors() throws Exception {
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new MethodArgumentNotValidException(null, bindingResult);
    testBindingResult(bindingResult, ex);
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MapBindingResult(org.springframework.validation.MapBindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletException(javax.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.Test)

Example 74 with BindingResult

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

the class DefaultErrorAttributesTests method extractBindingResultErrors.

@Test
public void extractBindingResultErrors() throws Exception {
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new BindException(bindingResult);
    testBindingResult(bindingResult, ex);
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException) MapBindingResult(org.springframework.validation.MapBindingResult) ServletException(javax.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.Test)

Example 75 with BindingResult

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

the class RelaxedDataBinderTests method testBindErrorNotWritableWithPrefix.

@Test
public void testBindErrorNotWritableWithPrefix() throws Exception {
    VanillaTarget target = new VanillaTarget();
    BindingResult result = bind(target, "spam: bar\n" + "vanilla.value: 123", "vanilla");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.getValue()).isEqualTo(123);
}
Also used : BindingResult(org.springframework.validation.BindingResult) 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