@@ -43,6 +43,7 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
4343 class ContainsAlphanumeric extends Constraint
4444 {
4545 public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
46+ public $mode = 'strict'; // If the constraint has configuration options, define them as public properties
4647 }
4748
4849 Add ``@Annotation `` or ``#[\Attribute] `` to the constraint class if you want to
@@ -160,7 +161,7 @@ You can use custom validators like the ones provided by Symfony itself:
160161 // ...
161162
162163 #[Assert\NotBlank]
163- #[AcmeAssert\ContainsAlphanumeric(options: ['mode' => ' loose'] )]
164+ #[AcmeAssert\ContainsAlphanumeric(mode: ' loose')]
164165 protected $name;
165166
166167 // ...
@@ -173,7 +174,8 @@ You can use custom validators like the ones provided by Symfony itself:
173174 properties :
174175 name :
175176 - NotBlank : ~
176- - App\Validator\ContainsAlphanumeric : ~
177+ - App\Validator\ContainsAlphanumeric :
178+ mode : ' loose'
177179
178180 .. code-block :: xml
179181
@@ -186,7 +188,9 @@ You can use custom validators like the ones provided by Symfony itself:
186188 <class name =" App\Entity\AcmeEntity" >
187189 <property name =" name" >
188190 <constraint name =" NotBlank" />
189- <constraint name =" App\Validator\ContainsAlphanumeric" />
191+ <constraint name =" App\Validator\ContainsAlphanumeric" >
192+ <option name =" mode" >loose</option >
193+ </constraint >
190194 </property >
191195 </class >
192196 </constraint-mapping >
@@ -207,7 +211,7 @@ You can use custom validators like the ones provided by Symfony itself:
207211 public static function loadValidatorMetadata(ClassMetadata $metadata)
208212 {
209213 $metadata->addPropertyConstraint('name', new NotBlank());
210- $metadata->addPropertyConstraint('name', new ContainsAlphanumeric());
214+ $metadata->addPropertyConstraint('name', new ContainsAlphanumeric(['mode' => 'loose'] ));
211215 }
212216 }
213217
0 commit comments