Get validation results with Spring Boot

How to get Form validation result in Spring Boot

Form

@Data public class SampleForm {  @NotBlank  @Size(max=100)  private String str; }

When BindingResult is declared as an argument of Controller method and the return value of hasErrors method is true This means that a Form validation error has occurred.

Controller

@Controller public class SampleController {  @RequestMapping(value = "/Sample", method = RequestMethod.POST)   public String sample(@Validated @ModelAttribute SampleForm form, BindingResult bindingResult, Model model) {    if (bindingResult.hasErrors()) {     List errorMessage = new ArrayList<>();     List errorList = bindingResult.getFieldErrors();     for (FieldError error : errorList) {      String field = "";      if (StringUtils.equals(error.getField(), "str")) { field = "input string";      }      if (StringUtils.equals(error.getCode(), "NotBlank")) { errorMessage.add (field + "not entered");      } else if (StringUtils.equals(error.getCode(), "Size")) { errorMessage.add (field + "number of digits is out of range");      }    }    model.addAttribute("errorList", errorMessage);

return "sampleHtml";   }  } }

Recommended Posts

Get validation results with Spring Boot
Get started with Spring boot
Form class validation test with Spring Boot
Self-made Validation with Spring
Download with Spring Boot
[Java] Article to add validation with Spring Boot 2.3.1.
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Spring Boot validation message changes
[Spring Boot] Get user information with Rest API (beginner)
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Easily get any metrics with Spring Boot + Micrometer + Prometheus Exporter
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
I tried to get started with Swagger using Spring Boot
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Processing at application startup with Spring Boot
[Introduction to Spring Boot] Form validation check
Hello World with Eclipse + Spring Boot + Maven
Perform transaction confirmation test with Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose
I tried Lazy Initialization with Spring Boot 2.2.0
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Asynchronous processing with Spring Boot using @Async
Implement paging function with Spring Boot + Thymeleaf
(IntelliJ + gradle) Hello World with Spring Boot
Use cache with EhCashe 2.x with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Achieve BASIC authentication with Spring Boot + Spring Security
Challenge Spring Boot
Spring Boot Form