How to create Custom Validation in Spring

In the post How to perform Form Validation with Spring Boot, we have already made a form validation. Today we’re gonna look at a way to create Custom Validation with that Form in Spring.

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Overview
1. Goal

Create a Custom Validation Annotation @ValidEmail that helps to validate field String email on server-side:

2. Project Structure


WebController is a Controller which has request mapping methods to display our page.
RequestInfo is a Data Model Class with annotation for validation.
ValidEmail is an Annotation Class refers to ValidEmailValidator class.
ValidEmailValidator is a Constraint Class which implements ConstraintValidator interface.
webapp folder contains all necessary JSP file and static resource.
application.properties contains settings for prefix and suffix for JSP files.
Dependencies for Spring Boot, Tomcat Embed Jasper and Common Validator in pom.xml.

3. Step to do

– Create Spring Boot project & add Dependencies
– Create jsp file and static resource
– Set prefix and suffix resolution
– Create @ValidEmail Annotation
– Create Constraint Class
– Create Data Model Class
– Create Web Controller
– Run Spring Boot Application & Enjoy Result

III. Practice
1. Create Spring Boot project & add Dependencies

Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields. Remember that we will the Packaging is War.

Click Next, then click Finish.

Open pom.xml and add Dependencies:

Note: (for another case)
If you create a Spring Starter Project with Jar Packaging, you should change

to

Then, extends @SpringBootApplication Class with SpringBootServletInitializer and add the code below:

2. Create jsp file and static resource

– Under src/main/webapp folder, create new folder named WEB-INF and jsps inside, then create:
request.jsp

successful.jsp

– Next, create main.css in webapp/css:

3. Set prefix and suffix resolution

Under src/main/resources, open application.properties and add:

4. Create @ValidEmail Annotation

Under package validation, create ValidEmail Annotation:

– We should specify which class is used. In this case, it will be ValidEmailValidator Class (in next step).

– For default message for notice and min characters for validating:

– We also indicate the contexts in which an annotation type is applicable and how long annotations with the annotated type are to be retained by:

In this example, @Target(FIELD) is enough.

5. Create Constraint Class

Under package validation, create ValidEmailValidator Class that implements ConstraintValidator:

– We check length of the email string by getting value in initialize() method (with method min() of ValidEmail annotation we declared before).

isValid() method returns boolean value that indicates the string which is annotated is passed or not.

– We also use EmailValidator of Common Validator to check if a string is in email format. Parameter false which is passed to getInstance() method tells EmailValidator object that a local address is not accepted.

6. Create Data Model Class

Under package model, create RequestInfo class with @ValidEmail and @Size(…) annotation for validating fields inside the class:

7. Create Web Controller

Under package controller, create WebController class:

@Valid annotation indicates that the Model should be validated.
@ModelAttribute(“requestInfo”) specifies the object that form will work with. It matches modelattribute in HTML or JSP file:

8. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Case 1:

Case 2:

Case 3:

Press Submit, the site will come to:

In Console Window, the System generates the String:

IV. Source Code

SpringFormCustomValidation


Related Posts



Got Something To Say:

Your email address will not be published. Required fields are marked *

*