Search in sources :

Example 81 with ObjectError

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

the class DefaultErrorAttributesTests method extractBindingResultErrorsExcludeMessageAndErrors.

@Test
void extractBindingResultErrorsExcludeMessageAndErrors() throws Exception {
    Method method = getClass().getDeclaredMethod("method", String.class);
    MethodParameter stringParam = new MethodParameter(method, 0);
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new WebExchangeBindException(stringParam, bindingResult);
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), ErrorAttributeOptions.defaults());
    assertThat(attributes).doesNotContainKey("message");
    assertThat(attributes).doesNotContainKey("errors");
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MapBindingResult(org.springframework.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Test(org.junit.jupiter.api.Test)

Example 82 with ObjectError

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

the class DefaultErrorAttributesTests method extractBindingResultErrors.

@Test
void extractBindingResultErrors() throws Exception {
    Method method = getClass().getDeclaredMethod("method", String.class);
    MethodParameter stringParam = new MethodParameter(method, 0);
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new WebExchangeBindException(stringParam, bindingResult);
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
    assertThat(attributes.get("message")).asString().startsWith("Validation failed for argument at index 0 in method: " + "int org.springframework.boot.web.reactive.error.DefaultErrorAttributesTests" + ".method(java.lang.String), with 1 error(s)");
    assertThat(attributes.get("errors")).isEqualTo(bindingResult.getAllErrors());
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MapBindingResult(org.springframework.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Test(org.junit.jupiter.api.Test)

Example 83 with ObjectError

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

the class ValidationErrorsTests method getErrorsShouldAdaptFieldErrorsToBeOriginProviders.

@Test
void getErrorsShouldAdaptFieldErrorsToBeOriginProviders() {
    Set<ConfigurationProperty> boundProperties = new LinkedHashSet<>();
    ConfigurationPropertyName name1 = ConfigurationPropertyName.of("foo.bar");
    Origin origin1 = MockOrigin.of("line1");
    boundProperties.add(new ConfigurationProperty(name1, "boot", origin1));
    ConfigurationPropertyName name2 = ConfigurationPropertyName.of("foo.baz.bar");
    Origin origin2 = MockOrigin.of("line2");
    boundProperties.add(new ConfigurationProperty(name2, "boot", origin2));
    List<ObjectError> allErrors = new ArrayList<>();
    allErrors.add(new FieldError("objectname", "bar", "message"));
    ValidationErrors errors = new ValidationErrors(ConfigurationPropertyName.of("foo.baz"), boundProperties, allErrors);
    assertThat(Origin.from(errors.getAllErrors().get(0))).isEqualTo(origin2);
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) LinkedHashSet(java.util.LinkedHashSet) Origin(org.springframework.boot.origin.Origin) MockOrigin(org.springframework.boot.origin.MockOrigin) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) ObjectError(org.springframework.validation.ObjectError) ArrayList(java.util.ArrayList) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 84 with ObjectError

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

the class ValidationErrorsTests method getErrorsShouldReturnErrors.

@Test
void getErrorsShouldReturnErrors() {
    List<ObjectError> allErrors = new ArrayList<>();
    allErrors.add(new ObjectError("foo", "bar"));
    ValidationErrors errors = new ValidationErrors(NAME, Collections.emptySet(), allErrors);
    assertThat(errors.getAllErrors()).isEqualTo(allErrors);
}
Also used : ObjectError(org.springframework.validation.ObjectError) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 85 with ObjectError

use of org.springframework.validation.ObjectError in project FS-Blog by JamesZBL.

the class UserController method fFrontUserLogin.

/**
 * 前台用户登录
 * 表单提交
 */
@PostMapping("/userlogin.f")
public String fFrontUserLogin(HttpServletRequest request, Model model, @Valid UserLoginForm loginForm, BindingResult bindingResult) throws Exception {
    if (bindingResult.hasErrors()) {
        List<ObjectError> errors = bindingResult.getAllErrors();
        addModelAtt(model, VIEW_MSG, errors.get(0).getDefaultMessage());
        return "userlogin";
    }
    User user = mUserService.loginAuthentication(loginForm);
    if (null != user) {
        mUserService.joinSession(request, user);
        return "redirect:/";
    }
    addModelAtt(model, VIEW_MSG, "用户名或密码错误");
    return "userlogin";
}
Also used : ObjectError(org.springframework.validation.ObjectError) User(me.zbl.fullstack.entity.User) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

ObjectError (org.springframework.validation.ObjectError)106 FieldError (org.springframework.validation.FieldError)19 BindingResult (org.springframework.validation.BindingResult)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 BindException (org.springframework.validation.BindException)14 ArrayList (java.util.ArrayList)13 MapBindingResult (org.springframework.validation.MapBindingResult)13 Test (org.junit.jupiter.api.Test)12 ModelAndView (org.springframework.web.servlet.ModelAndView)10 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)7 Test (org.junit.Test)6 BusinessRuleException (org.mifos.service.BusinessRuleException)6 Errors (org.springframework.validation.Errors)6 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)5 LocationMonitorIdCommand (org.opennms.web.svclayer.model.LocationMonitorIdCommand)5 Document (org.w3c.dom.Document)5 Element (org.w3c.dom.Element)5