Form Data Validation

Learn how to use the Hibernate Validator with Spring MVC application.

Form data validation is the process of ensuring that the data submitted through a web form is accurate, complete, and adheres to specified rules before it is processed or stored. It prevents invalid data from causing errors or inconsistencies in an application. Data validation includes checking if the required fields are not left blank, numbers are within a given range, and data is entered in the correct format. The Standard Bean Validation API is the preferred approach for validation in Spring applications. We will use the Hibernate implementation of the API known as Hibernate Validator.

Enabling validation

To enable validation in a Spring Boot application, we need to add the spring-boot-starter-validation dependency. This starter includes support for Java Bean Validation with Hibernate Validator. Spring Boot will automatically bootstrap Hibernate Validator which is the default implementation of the JSR 380 Bean Validation specification.

Press + to interact
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>

This dependency lets Spring MVC detect and enable validation support provided by the bean validation API. We can now use the JSR 380 validation annotations. ...