Spring MVC Form Validation

The need for validation checking whether or not the user input provided is valid is not like:
    • Required fields
    • Valid numbers between some range specified
    • Valid format(postal code)
    • Custom business rule
Java’s Standard Bean Validation API:
   • Java has a standard Bean Validation API
   • Defines information and API for entity validation
   • Not tied to either the online tier or the persistence tier
   • Available for server-side applications and additionally client-side Java FX/Swing apps
   • https://beanvalidation.org you undergo this website for obtaining a lot of info.
   • Only a Specification vendor independent
   • Hibernate started as an ORM project
   • In recent years, they have expanded into alternative areas
Spring and Validation:
   • Bean Validation API supports Spring version 4 onwards
   • The preferred technique for validation once building Spring apps
   • Simply add the Validation JARs library to the project
Bean Validation Features:
   • Required
   • Validate length
   • Validate numbers
   • Validate with regular expressions
   • Custom validation
Validation Annotations:

Annotation             Description
@NotNull    Checks the annotated value is not null
@Min    Must be a number >=value
@Max    Must be a number<=value
@Size    The size must match the given size
@Pattern    Must match a regular expression pattern
@Future/@PastThe date must be in future or past of the given date
others… 

Some of the primarily used validations are:

@Min(value="18"  message="Age must be greater than 18 years")
private int age;

→ @Min(value=): Checks if the value is greater than or equal to the specified value.

@Max(value="30"  message="Age must be less than 30 years")
private int age;

→ @Max(value=): Checks if the value is smaller than or equal to the specified value.

@NotNull(message="Name must not be null")
private String name;

→ @NotNull: Checks the value is not null.

@NotBlank(message="Name must not be null and contains 1 character or more characters")
private String name;

 → @NotBlank: Check if the annotated string is not null and the trimmed length is greater than 0.

@NotEmpty(message="Name must not be null and must have size is greater than 0")
private String name;

→ @NotEmpty: Check if the annotated element is not null and not empty.

@Email(message="Email should be valid")
private String email;

→ @Email: Check if the specified string is a valid email address.

@Pattern(regexp="^[a-zA-z0-9]{8,15}$")
private String password;

→ @Pattern(regexp= “ ” flags= “ ”):  Check if the annotated string matches with a regular expression and consider matches with the given flag.

@Size(min="10" max="300" message="Description must be between 10 and 300 characters")
private String desc;

→ @Size: Check if the annotated element’s size is between the range from the min value to the max value specified.

@AssertFalse
private boolean isAvailable;

→ @AssertFalse: Check if the annotated element is false.

@AssertTrue
private boolean isAvailable;

→ @AssertTrue: Check the annotated element is true.

@Positive
private int value;

→ @Positive: Check if the value is greater than 0.

@PositiveOrZero
private int value;

→ @PositiveOrZero: Check if the value is greater than or equal to 0.

@Negative
private int value;

→ @Negative: Check if the value is smaller than 0.

@NegativeOrZero
private int value;

→ @NegativeOrZero: Check if the value is smaller than or equal to 0.

@Null
private String value;

→ @Null: Check if the annotated value is null.

We can undergo the jar library of the Hibernate Validator from this site https://hibernate.org/validator

spring_mvc_form_validation

We can download hibernate validator 6.x compatible with Spring 5.

Conclusion:
This topic is explained What is the need for the validator in the Spring MVC  Form? What are the features of Bean Validator? How can we download Hibernate Validator? What are the various annotations for validators?

Leave a Comment