Spring MVC Validations
Spring MVC Validations
Spring has provided inbuilt validations framework. Since we are using Spring
framework validation implementation, we will have to use Spring Form tags to get
the errors and set the form bean and variable names.
@NotEmpty(message = "First name cannot be null and must have size greater
than 0")
private String firstName;
@NotNull(message = "Second name must not be null, empty value/space can be
considered")
private String lastName;
@Pattern(regexp = "^[a-zA-Z0-9]{6,10}$")
private String password;
8. @AssertFalse – Checks that the annotated element is false.
9. @AssertTrue – Checks that the annotated element is true.
@AssertTrue
private boolean isWorking;
@AssertFalse
private boolean isStudent;
10. @NegativeOrZero – Checks if the given element is smaller than or equal to
0.
11. @Null – Checks that the annotated value is null.
12. @Negative – Checks if the element is strictly smaller than 0.
13. @Positive – Checks if the element is strictly greater than 0.
14. @PositiveOrZero – Checks if the given element is greater than or equal to 0.
@Positive
private int operand1;
@Negative
private int operand2;
@PositiveOrZero
private int operand3
@NegativeOrZero
private int operand4;
@Null
private int nullVal;
15. @Size – Checks if the annotated element’s size is between min value and
max value provided (inclusive).
@Size(min = 10, max = 200, message = "About Me must be between 10 and 200
characters")
private String aboutMe;