Description

Use this when you want to use a set of constraints in multiple places.

Syntax

namespace App\Validator;
 
use Symfony\Component\Validator\Constraints\Compound;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotCompromisedPassword;
use Symfony\Component\Validator\Constraints\Type;
 
/**
 * @Annotation
 */
class MatchesPasswordRequirements extends Compound
{
    protected function getConstraints(array $options): array
    {
        return [
            new NotBlank(),
            new Type('string'),
            new Length(['min' => 12]),
            new NotCompromisedPassword(),
        ];
    }
}
namespace App\Dto;
 
// ...
use App\Validator\MatchesPasswordRequirements;
 
class ChangePasswordDto
{
    /**
     * @MatchesPasswordRequirements
     */
    private $newPassword;
 
    // ...
}