diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index e320cd5e45a..c061c23f682 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -38,54 +38,27 @@ use it as an annotation/attribute in other classes. The ``#[HasNamedArguments]`` attribute was introduced in Symfony 6.1. -You can use ``#[HasNamedArguments]`` or ``getRequiredOptions()`` to make some constraint options required: +You can use ``#[HasNamedArguments]`` or ``getRequiredOptions()`` to make some constraint options required:: -.. configuration-block:: - - .. code-block:: php-annotations - - // src/Validator/ContainsAlphanumeric.php - namespace App\Validator; - - use Symfony\Component\Validator\Constraint; - - /** - * @Annotation - */ - class ContainsAlphanumeric extends Constraint - { - public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.'; - public $mode; - - public function getRequiredOptions(): array - { - return ['mode']; - } - } - - .. code-block:: php-attributes + // src/Validator/ContainsAlphanumeric.php + namespace App\Validator; - // src/Validator/ContainsAlphanumeric.php - namespace App\Validator; + use Symfony\Component\Validator\Attribute\HasNamedArguments; + use Symfony\Component\Validator\Constraint; - use Symfony\Component\Validator\Attribute\HasNamedArguments; - use Symfony\Component\Validator\Constraint; + #[\Attribute] + class ContainsAlphanumeric extends Constraint + { + public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.'; + public string $mode; - #[\Attribute] - class ContainsAlphanumeric extends Constraint + #[HasNamedArguments] + public function __construct(string $mode, array $groups = null, mixed $payload = null) { - public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.'; - - public string $mode; - - #[HasNamedArguments] - public function __construct(string $mode, array $groups = null, mixed $payload = null) - { - parent::__construct([], $groups, $payload); - - $this->mode = $mode; - } + parent::__construct([], $groups, $payload); + $this->mode = $mode; } + } Creating the Validator itself -----------------------------