@@ -48,6 +48,59 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
48
48
Add ``@Annotation `` or ``#[\Attribute] `` to the constraint class if you want to
49
49
use it as an annotation/attribute in other classes.
50
50
51
+ .. versionadded :: 6.1
52
+
53
+ The ``#[HasNamedArguments] `` attribute was introduced in Symfony 6.1.
54
+
55
+ You can use ``#[HasNamedArguments] `` or ``getRequiredOptions() `` to make some constraint options required:
56
+
57
+ .. configuration-block ::
58
+
59
+ .. code-block :: php-annotations
60
+
61
+ // src/Validator/ContainsAlphanumeric.php
62
+ namespace App\Validator;
63
+
64
+ use Symfony\Component\Validator\Constraint;
65
+
66
+ /**
67
+ * @Annotation
68
+ */
69
+ class ContainsAlphanumeric extends Constraint
70
+ {
71
+ public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
72
+ public $mode;
73
+
74
+ public function getRequiredOptions(): array
75
+ {
76
+ return ['mode'];
77
+ }
78
+ }
79
+
80
+ .. code-block :: php-attributes
81
+
82
+ // src/Validator/ContainsAlphanumeric.php
83
+ namespace App\Validator;
84
+
85
+ use Symfony\Component\Validator\Attribute\HasNamedArguments;
86
+ use Symfony\Component\Validator\Constraint;
87
+
88
+ #[\Attribute]
89
+ class ContainsAlphanumeric extends Constraint
90
+ {
91
+ public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
92
+
93
+ public string $mode;
94
+
95
+ #[HasNamedArguments]
96
+ public function __construct(string $mode, array $groups = null, mixed $payload = null)
97
+ {
98
+ parent::__construct([], $groups, $payload);
99
+
100
+ $this->mode = $mode;
101
+ }
102
+ }
103
+
51
104
Creating the Validator itself
52
105
-----------------------------
53
106
@@ -271,7 +324,7 @@ not to the property:
271
324
namespace App\Entity;
272
325
273
326
use App\Validator as AcmeAssert;
274
-
327
+
275
328
/**
276
329
* @AcmeAssert\ProtocolClass
277
330
*/
0 commit comments