@@ -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
@@ -213,7 +214,7 @@ You can use custom validators like the ones provided by Symfony itself:
213214 // ...
214215
215216 #[Assert\NotBlank]
216- #[AcmeAssert\ContainsAlphanumeric(options: ['mode' => ' loose'] )]
217+ #[AcmeAssert\ContainsAlphanumeric(mode: ' loose')]
217218 protected $name;
218219
219220 // ...
@@ -226,7 +227,8 @@ You can use custom validators like the ones provided by Symfony itself:
226227 properties :
227228 name :
228229 - NotBlank : ~
229- - App\Validator\ContainsAlphanumeric : ~
230+ - App\Validator\ContainsAlphanumeric :
231+ mode : ' loose'
230232
231233 .. code-block :: xml
232234
@@ -239,7 +241,9 @@ You can use custom validators like the ones provided by Symfony itself:
239241 <class name =" App\Entity\AcmeEntity" >
240242 <property name =" name" >
241243 <constraint name =" NotBlank" />
242- <constraint name =" App\Validator\ContainsAlphanumeric" />
244+ <constraint name =" App\Validator\ContainsAlphanumeric" >
245+ <option name =" mode" >loose</option >
246+ </constraint >
243247 </property >
244248 </class >
245249 </constraint-mapping >
@@ -260,7 +264,7 @@ You can use custom validators like the ones provided by Symfony itself:
260264 public static function loadValidatorMetadata(ClassMetadata $metadata)
261265 {
262266 $metadata->addPropertyConstraint('name', new NotBlank());
263- $metadata->addPropertyConstraint('name', new ContainsAlphanumeric());
267+ $metadata->addPropertyConstraint('name', new ContainsAlphanumeric(['mode' => 'loose'] ));
264268 }
265269 }
266270
0 commit comments