. */ namespace App\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; class ValidThemeValidator extends ConstraintValidator { public function __construct(private readonly array $available_themes) { } public function validate($value, Constraint $constraint): void { if (!$constraint instanceof ValidTheme) { throw new UnexpectedTypeException($constraint, ValidTheme::class); } //Empty values are allowed if (null === $value || '' === $value) { return; } //If a value is set, it must be a value from the available themes list if (!in_array($value, $this->available_themes, true)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $value) ->addViolation(); } } }