Java Code Examples for org.springframework.validation.Errors#getFieldError()
The following examples show how to use
org.springframework.validation.Errors#getFieldError() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PostsController.java From blog-spring with MIT License | 5 votes |
@PostMapping("") @ResponseBody public JSONReplyDTO create(@Valid @RequestBody PostForm postForm, Errors errors) { if (errors.hasFieldErrors()) { FieldError error = errors.getFieldError(); return JSONReplyDTO.fail(error.getField() + ": " + error.getDefaultMessage()); } postService.create(postForm); return JSONReplyDTO.success("Created successfully"); }
Example 2
Source File: PostsController.java From blog-spring with MIT License | 5 votes |
@PutMapping("/{id}") @ResponseBody @Transactional public JSONReplyDTO update( @PathVariable("id") Post post, @Valid @RequestBody PostForm postForm, Errors errors) { if (errors.hasFieldErrors()) { FieldError error = errors.getFieldError(); return JSONReplyDTO.fail(error.getField() + ": " + error.getDefaultMessage()); } postService.update(post, postForm); return JSONReplyDTO.success("Updated successfully"); }
Example 3
Source File: CargoTrackingControllerTest.java From dddsample-core with MIT License | 5 votes |
@Test public void cannnotGetUnknownCargo() throws Exception { String trackingId = "UNKNOWN"; Map<String, Object> model = mockMvc.perform(post("/track").param("trackingId", trackingId)).andReturn().getModelAndView().getModel(); Errors errors = (Errors) model.get(BindingResult.MODEL_KEY_PREFIX + "trackCommand"); FieldError fe = errors.getFieldError("trackingId"); assertThat(fe.getCode()).isEqualTo("cargo.unknown_id"); assertThat(fe.getArguments().length).isEqualTo(1); assertThat(fe.getArguments()[0]).isEqualTo(trackingId); }
Example 4
Source File: RegistrationValidatorTest.java From codenjoy with GNU General Public License v3.0 | 5 votes |
private void assertError(Errors errors, String field, String expectedCode) { assertTrue(errors.getErrorCount() > 0); FieldError fieldError = errors.getFieldError(field); assertNotNull("Error field must have been set", fieldError); assertTrue(String.format("Missing expected error code for field '%s'\nExpected: '%s', actual ones: %s", field, expectedCode, Arrays.toString(fieldError.getCodes())), Arrays.asList(fieldError.getCodes()).contains(expectedCode)); }
Example 5
Source File: EscapedErrorsTests.java From spring-analysis-note with MIT License | 4 votes |
@Test public void testEscapedErrors() { TestBean tb = new TestBean(); tb.setName("empty &"); Errors errors = new EscapedErrors(new BindException(tb, "tb")); errors.rejectValue("name", "NAME_EMPTY &", null, "message: &"); errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>"); errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>"); errors.reject("GENERAL_ERROR \" '", null, "message: \" '"); assertTrue("Correct errors flag", errors.hasErrors()); assertTrue("Correct number of errors", errors.getErrorCount() == 4); assertTrue("Correct object name", "tb".equals(errors.getObjectName())); assertTrue("Correct global errors flag", errors.hasGlobalErrors()); assertTrue("Correct number of global errors", errors.getGlobalErrorCount() == 1); ObjectError globalError = errors.getGlobalError(); String defaultMessage = globalError.getDefaultMessage(); assertTrue("Global error message escaped", "message: " '".equals(defaultMessage)); assertTrue("Global error code not escaped", "GENERAL_ERROR \" '".equals(globalError.getCode())); ObjectError globalErrorInList = errors.getGlobalErrors().get(0); assertTrue("Same global error in list", defaultMessage.equals(globalErrorInList.getDefaultMessage())); ObjectError globalErrorInAllList = errors.getAllErrors().get(3); assertTrue("Same global error in list", defaultMessage.equals(globalErrorInAllList.getDefaultMessage())); assertTrue("Correct field errors flag", errors.hasFieldErrors()); assertTrue("Correct number of field errors", errors.getFieldErrorCount() == 3); assertTrue("Correct number of field errors in list", errors.getFieldErrors().size() == 3); FieldError fieldError = errors.getFieldError(); assertTrue("Field error code not escaped", "NAME_EMPTY &".equals(fieldError.getCode())); assertTrue("Field value escaped", "empty &".equals(errors.getFieldValue("name"))); FieldError fieldErrorInList = errors.getFieldErrors().get(0); assertTrue("Same field error in list", fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage())); assertTrue("Correct name errors flag", errors.hasFieldErrors("name")); assertTrue("Correct number of name errors", errors.getFieldErrorCount("name") == 1); assertTrue("Correct number of name errors in list", errors.getFieldErrors("name").size() == 1); FieldError nameError = errors.getFieldError("name"); assertTrue("Name error message escaped", "message: &".equals(nameError.getDefaultMessage())); assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode())); assertTrue("Name value escaped", "empty &".equals(errors.getFieldValue("name"))); FieldError nameErrorInList = errors.getFieldErrors("name").get(0); assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage())); assertTrue("Correct age errors flag", errors.hasFieldErrors("age")); assertTrue("Correct number of age errors", errors.getFieldErrorCount("age") == 2); assertTrue("Correct number of age errors in list", errors.getFieldErrors("age").size() == 2); FieldError ageError = errors.getFieldError("age"); assertTrue("Age error message escaped", "message: <tag>".equals(ageError.getDefaultMessage())); assertTrue("Age error code not escaped", "AGE_NOT_SET <tag>".equals(ageError.getCode())); assertTrue("Age value not escaped", (new Integer(0)).equals(errors.getFieldValue("age"))); FieldError ageErrorInList = errors.getFieldErrors("age").get(0); assertTrue("Same name error in list", ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage())); FieldError ageError2 = errors.getFieldErrors("age").get(1); assertTrue("Age error 2 message escaped", "message: <tag>".equals(ageError2.getDefaultMessage())); assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode())); }
Example 6
Source File: CustomValidatorFactoryBean.java From openemm with GNU Affero General Public License v3.0 | 4 votes |
private boolean checkNoBindingFailure(Errors errors, String field) { FieldError error = errors.getFieldError(field); return error == null || !error.isBindingFailure(); }
Example 7
Source File: EscapedErrorsTests.java From java-technology-stack with MIT License | 4 votes |
@Test public void testEscapedErrors() { TestBean tb = new TestBean(); tb.setName("empty &"); Errors errors = new EscapedErrors(new BindException(tb, "tb")); errors.rejectValue("name", "NAME_EMPTY &", null, "message: &"); errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>"); errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>"); errors.reject("GENERAL_ERROR \" '", null, "message: \" '"); assertTrue("Correct errors flag", errors.hasErrors()); assertTrue("Correct number of errors", errors.getErrorCount() == 4); assertTrue("Correct object name", "tb".equals(errors.getObjectName())); assertTrue("Correct global errors flag", errors.hasGlobalErrors()); assertTrue("Correct number of global errors", errors.getGlobalErrorCount() == 1); ObjectError globalError = errors.getGlobalError(); String defaultMessage = globalError.getDefaultMessage(); assertTrue("Global error message escaped", "message: " '".equals(defaultMessage)); assertTrue("Global error code not escaped", "GENERAL_ERROR \" '".equals(globalError.getCode())); ObjectError globalErrorInList = errors.getGlobalErrors().get(0); assertTrue("Same global error in list", defaultMessage.equals(globalErrorInList.getDefaultMessage())); ObjectError globalErrorInAllList = errors.getAllErrors().get(3); assertTrue("Same global error in list", defaultMessage.equals(globalErrorInAllList.getDefaultMessage())); assertTrue("Correct field errors flag", errors.hasFieldErrors()); assertTrue("Correct number of field errors", errors.getFieldErrorCount() == 3); assertTrue("Correct number of field errors in list", errors.getFieldErrors().size() == 3); FieldError fieldError = errors.getFieldError(); assertTrue("Field error code not escaped", "NAME_EMPTY &".equals(fieldError.getCode())); assertTrue("Field value escaped", "empty &".equals(errors.getFieldValue("name"))); FieldError fieldErrorInList = errors.getFieldErrors().get(0); assertTrue("Same field error in list", fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage())); assertTrue("Correct name errors flag", errors.hasFieldErrors("name")); assertTrue("Correct number of name errors", errors.getFieldErrorCount("name") == 1); assertTrue("Correct number of name errors in list", errors.getFieldErrors("name").size() == 1); FieldError nameError = errors.getFieldError("name"); assertTrue("Name error message escaped", "message: &".equals(nameError.getDefaultMessage())); assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode())); assertTrue("Name value escaped", "empty &".equals(errors.getFieldValue("name"))); FieldError nameErrorInList = errors.getFieldErrors("name").get(0); assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage())); assertTrue("Correct age errors flag", errors.hasFieldErrors("age")); assertTrue("Correct number of age errors", errors.getFieldErrorCount("age") == 2); assertTrue("Correct number of age errors in list", errors.getFieldErrors("age").size() == 2); FieldError ageError = errors.getFieldError("age"); assertTrue("Age error message escaped", "message: <tag>".equals(ageError.getDefaultMessage())); assertTrue("Age error code not escaped", "AGE_NOT_SET <tag>".equals(ageError.getCode())); assertTrue("Age value not escaped", (new Integer(0)).equals(errors.getFieldValue("age"))); FieldError ageErrorInList = errors.getFieldErrors("age").get(0); assertTrue("Same name error in list", ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage())); FieldError ageError2 = errors.getFieldErrors("age").get(1); assertTrue("Age error 2 message escaped", "message: <tag>".equals(ageError2.getDefaultMessage())); assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode())); }
Example 8
Source File: EscapedErrorsTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test public void testEscapedErrors() { TestBean tb = new TestBean(); tb.setName("empty &"); Errors errors = new EscapedErrors(new BindException(tb, "tb")); errors.rejectValue("name", "NAME_EMPTY &", null, "message: &"); errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>"); errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>"); errors.reject("GENERAL_ERROR \" '", null, "message: \" '"); assertTrue("Correct errors flag", errors.hasErrors()); assertTrue("Correct number of errors", errors.getErrorCount() == 4); assertTrue("Correct object name", "tb".equals(errors.getObjectName())); assertTrue("Correct global errors flag", errors.hasGlobalErrors()); assertTrue("Correct number of global errors", errors.getGlobalErrorCount() == 1); ObjectError globalError = errors.getGlobalError(); assertTrue("Global error message escaped", "message: " '".equals(globalError.getDefaultMessage())); assertTrue("Global error code not escaped", "GENERAL_ERROR \" '".equals(globalError.getCode())); ObjectError globalErrorInList = errors.getGlobalErrors().get(0); assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInList.getDefaultMessage())); ObjectError globalErrorInAllList = errors.getAllErrors().get(3); assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInAllList.getDefaultMessage())); assertTrue("Correct field errors flag", errors.hasFieldErrors()); assertTrue("Correct number of field errors", errors.getFieldErrorCount() == 3); assertTrue("Correct number of field errors in list", errors.getFieldErrors().size() == 3); FieldError fieldError = errors.getFieldError(); assertTrue("Field error code not escaped", "NAME_EMPTY &".equals(fieldError.getCode())); assertTrue("Field value escaped", "empty &".equals(errors.getFieldValue("name"))); FieldError fieldErrorInList = errors.getFieldErrors().get(0); assertTrue("Same field error in list", fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage())); assertTrue("Correct name errors flag", errors.hasFieldErrors("name")); assertTrue("Correct number of name errors", errors.getFieldErrorCount("name") == 1); assertTrue("Correct number of name errors in list", errors.getFieldErrors("name").size() == 1); FieldError nameError = errors.getFieldError("name"); assertTrue("Name error message escaped", "message: &".equals(nameError.getDefaultMessage())); assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode())); assertTrue("Name value escaped", "empty &".equals(errors.getFieldValue("name"))); FieldError nameErrorInList = errors.getFieldErrors("name").get(0); assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage())); assertTrue("Correct age errors flag", errors.hasFieldErrors("age")); assertTrue("Correct number of age errors", errors.getFieldErrorCount("age") == 2); assertTrue("Correct number of age errors in list", errors.getFieldErrors("age").size() == 2); FieldError ageError = errors.getFieldError("age"); assertTrue("Age error message escaped", "message: <tag>".equals(ageError.getDefaultMessage())); assertTrue("Age error code not escaped", "AGE_NOT_SET <tag>".equals(ageError.getCode())); assertTrue("Age value not escaped", (new Integer(0)).equals(errors.getFieldValue("age"))); FieldError ageErrorInList = errors.getFieldErrors("age").get(0); assertTrue("Same name error in list", ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage())); FieldError ageError2 = errors.getFieldErrors("age").get(1); assertTrue("Age error 2 message escaped", "message: <tag>".equals(ageError2.getDefaultMessage())); assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode())); }