In form validation, @Valid tells Spring to check a submitted form object against its Bean Validation constraints before your handler logic runs. Constraints such as @NotBlank, @Size and @Email sit on the form class fields. @Valid on the controller parameter triggers them, and the results land in a BindingResult that your template can show next to each field.
This article builds a complete server-rendered form with Spring Boot 3 or later, Thymeleaf and jakarta.validation, including the redirect-after-post pattern and custom constraints.
When a browser posts a form, Spring binds the request parameters to a form object (a DTO annotated with constraints). If the parameter is annotated with @Valid, the configured Validator runs right after binding. Binding errors (for example text in a numeric field) and constraint violations are both collected as field errors.
If you declare a BindingResult right after the form parameter, Spring puts the errors there and still calls your method, so you can redisplay the form. Without it, Spring throws an exception and the user sees an error page.
Use a dedicated form class rather than a JPA entity, so web constraints stay separate from persistence. A record works well on Java 17 and later, since Spring binds records through their canonical constructor:
Add spring-boot-starter-validation so Hibernate Validator is on the classpath. Spring Boot 3 and 4 use the jakarta.validation package; the older javax.validation imports from Spring Boot 2 tutorials will not compile.
Returning the view name directly on error keeps the entered values and error messages. On success, redirect so a browser refresh does not resubmit the form.
th:field binds each input to the form object, and th:errors prints the message from the failing constraint. Thymeleaf adds the CSRF token automatically when Spring Security is present.
For rules the built-in annotations do not cover, define your own constraint and validator.
Return true for null so each constraint checks one thing, and combine it with @NotBlank when the value is required. For rules across fields, such as password confirmation, put a class-level constraint on the form.
spring-boot-starter-validation, so constraints are never evaluated.BindingResult after another parameter instead of directly after the validated object.Yes. th:errors only displays errors already stored in the BindingResult. @Valid is what creates them.
Spring associates a BindingResult with the argument that precedes it. Another parameter in between causes an error at request time.
Yes, on a @ModelAttribute object that binds query parameters. For individual simple parameters, put constraints on the parameter and rely on method validation in Spring Framework 6.1 and later.
Set message on the constraint, or use {key} placeholders resolved from ValidationMessages.properties or a Spring MessageSource for translations.
@Valid runs Bean Validation on a bound form object, and BindingResult lets you redisplay the form with field-level messages. Use a separate form class or record, import from jakarta.validation, redirect after success, and write custom constraints for rules that built-in annotations cannot express.