@@ -43,6 +43,7 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
43
43
class ContainsAlphanumeric extends Constraint
44
44
{
45
45
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
46
47
}
47
48
48
49
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:
213
214
// ...
214
215
215
216
#[Assert\NotBlank]
216
- #[AcmeAssert\ContainsAlphanumeric(options: ['mode' => ' loose'] )]
217
+ #[AcmeAssert\ContainsAlphanumeric(mode: ' loose')]
217
218
protected $name;
218
219
219
220
// ...
@@ -226,7 +227,8 @@ You can use custom validators like the ones provided by Symfony itself:
226
227
properties :
227
228
name :
228
229
- NotBlank : ~
229
- - App\Validator\ContainsAlphanumeric : ~
230
+ - App\Validator\ContainsAlphanumeric :
231
+ mode : ' loose'
230
232
231
233
.. code-block :: xml
232
234
@@ -239,7 +241,9 @@ You can use custom validators like the ones provided by Symfony itself:
239
241
<class name =" App\Entity\AcmeEntity" >
240
242
<property name =" name" >
241
243
<constraint name =" NotBlank" />
242
- <constraint name =" App\Validator\ContainsAlphanumeric" />
244
+ <constraint name =" App\Validator\ContainsAlphanumeric" >
245
+ <option name =" mode" >loose</option >
246
+ </constraint >
243
247
</property >
244
248
</class >
245
249
</constraint-mapping >
@@ -260,7 +264,7 @@ You can use custom validators like the ones provided by Symfony itself:
260
264
public static function loadValidatorMetadata(ClassMetadata $metadata)
261
265
{
262
266
$metadata->addPropertyConstraint('name', new NotBlank());
263
- $metadata->addPropertyConstraint('name', new ContainsAlphanumeric());
267
+ $metadata->addPropertyConstraint('name', new ContainsAlphanumeric(['mode' => 'loose'] ));
264
268
}
265
269
}
266
270
0 commit comments