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");
}
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());
}
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);
}
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);
}
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";
}
Aggregations