@@ -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
@@ -166,7 +167,7 @@ You can use custom validators like the ones provided by Symfony itself:
166167 // ...
167168
168169 #[Assert\NotBlank]
169- #[AcmeAssert\ContainsAlphanumeric(options: ['mode' => ' loose'] )]
170+ #[AcmeAssert\ContainsAlphanumeric(mode: ' loose')]
170171 protected $name;
171172
172173 // ...
@@ -179,7 +180,8 @@ You can use custom validators like the ones provided by Symfony itself:
179180 properties :
180181 name :
181182 - NotBlank : ~
182- - App\Validator\ContainsAlphanumeric : ~
183+ - App\Validator\ContainsAlphanumeric :
184+ mode : ' loose'
183185
184186 .. code-block :: xml
185187
@@ -192,7 +194,9 @@ You can use custom validators like the ones provided by Symfony itself:
192194 <class name =" App\Entity\AcmeEntity" >
193195 <property name =" name" >
194196 <constraint name =" NotBlank" />
195- <constraint name =" App\Validator\ContainsAlphanumeric" />
197+ <constraint name =" App\Validator\ContainsAlphanumeric" >
198+ <option name =" mode" >loose</option >
199+ </constraint >
196200 </property >
197201 </class >
198202 </constraint-mapping >
@@ -213,7 +217,7 @@ You can use custom validators like the ones provided by Symfony itself:
213217 public static function loadValidatorMetadata(ClassMetadata $metadata)
214218 {
215219 $metadata->addPropertyConstraint('name', new NotBlank());
216- $metadata->addPropertyConstraint('name', new ContainsAlphanumeric());
220+ $metadata->addPropertyConstraint('name', new ContainsAlphanumeric(['mode' => 'loose'] ));
217221 }
218222 }
219223
0 commit comments