Custom validations

More complex custom validations can be implemented using the @AssertTrue annotation. The following list an example method added to the User class:

    @AssertTrue(message = "Password fields don't match")     private boolean isValid() {       return this.password.equals(this.password2);     }

@AssertTrue(message = "Password fields don't match") is the message to be shown if the validation fails.

Any complex validation logic with multiple fields can be implemented in these methods.

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.