@@ -38,54 +38,27 @@ use it as an annotation/attribute in other classes.
38
38
39
39
The ``#[HasNamedArguments] `` attribute was introduced in Symfony 6.1.
40
40
41
- You can use ``#[HasNamedArguments] `` or ``getRequiredOptions() `` to make some constraint options required:
41
+ You can use ``#[HasNamedArguments] `` or ``getRequiredOptions() `` to make some constraint options required::
42
42
43
- .. configuration-block ::
44
-
45
- .. code-block :: php-annotations
46
-
47
- // src/Validator/ContainsAlphanumeric.php
48
- namespace App\Validator;
49
-
50
- use Symfony\Component\Validator\Constraint;
51
-
52
- /**
53
- * @Annotation
54
- */
55
- class ContainsAlphanumeric extends Constraint
56
- {
57
- public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
58
- public $mode;
59
-
60
- public function getRequiredOptions(): array
61
- {
62
- return ['mode'];
63
- }
64
- }
65
-
66
- .. code-block :: php-attributes
43
+ // src/Validator/ContainsAlphanumeric.php
44
+ namespace App\Validator;
67
45
68
- // src/ Validator/ContainsAlphanumeric.php
69
- namespace App\ Validator;
46
+ use Symfony\Component\ Validator\Attribute\HasNamedArguments;
47
+ use Symfony\Component\ Validator\Constraint ;
70
48
71
- use Symfony\Component\Validator\Attribute\HasNamedArguments;
72
- use Symfony\Component\Validator\Constraint;
49
+ #[\Attribute]
50
+ class ContainsAlphanumeric extends Constraint
51
+ {
52
+ public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
53
+ public string $mode;
73
54
74
- #[\Attribute ]
75
- class ContainsAlphanumeric extends Constraint
55
+ #[HasNamedArguments ]
56
+ public function __construct(string $mode, array $groups = null, mixed $payload = null)
76
57
{
77
- public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
78
-
79
- public string $mode;
80
-
81
- #[HasNamedArguments]
82
- public function __construct(string $mode, array $groups = null, mixed $payload = null)
83
- {
84
- parent::__construct([], $groups, $payload);
85
-
86
- $this->mode = $mode;
87
- }
58
+ parent::__construct([], $groups, $payload);
59
+ $this->mode = $mode;
88
60
}
61
+ }
89
62
90
63
Creating the Validator itself
91
64
-----------------------------
0 commit comments