Validation Groups and Order

Learn how to influence the processing order of the validations by using validation groups.

In the Custom validator lesson, we had to ensure that the email was not empty in our custom validator due to the undefined order of the validations. We can influence the processing order of the validations by using validation groups.

Suppose we add some extra validations on CreateUserFormData, for example:

Press + to interact
@NotExistingUser(groups = ValidationGroupTwo.class)
public class CreateUserFormData {
@NotBlank
@Size(min = 1, max = 200, groups = ValidationGroupOne.class)
private String firstName;
@NotBlank
@Size(min = 1, max = 200, groups = ValidationGroupOne.class)
private String lastName;
@NotNull
private Gender gender;
@NotBlank
@Email(groups = ValidationGroupOne.class)
private String email;
@NotNull
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthday;
@NotBlank
@Pattern(regexp = "[0-9.\\-() x/+]+", groups = ValidationGroupOne.class)
private String phoneNumber;

Without using validation groups, all ...