How to Sequentially Apply Validation Groups

In some cases, you want to validate your groups by steps. To do this, you can
use the GroupSequence feature. In this case, an object defines a group
sequence, which determines the order groups should be validated.

For example, suppose you have a User class and want to validate that the
username and the password are different only if all other validation passes
(in order to avoid multiple error messages).

In this example, it will first validate all constraints in the group User
(which is the same as the Default group). Only if all constraints in
that group are valid, the second group, Strict, will be validated.

Caution

As you have already seen in the previous section, the Default group
and the group containing the class name (e.g. User) were identical.
However, when using Group Sequences, they are no longer identical. The
Default group will now reference the group sequence, instead of all
constraints that do not belong to any group.

This means that you have to use the {ClassName} (e.g. User) group
when specifying a group sequence. When using Default, you get an
infinite recursion (as the Default group references the group
sequence, which will contain the Default group which references the
same group sequence, ...).

Imagine a User entity which can be a normal user or a premium user. When
it's a premium user, some extra constraints should be added to the user entity
(e.g. the credit card details). To dynamically determine which groups should
be activated, you can create a Group Sequence Provider. First, create the
entity and a new constraint group called Premium: