@@ -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
@@ -166,7 +167,7 @@ You can use custom validators like the ones provided by Symfony itself:
166
167
// ...
167
168
168
169
#[Assert\NotBlank]
169
- #[AcmeAssert\ContainsAlphanumeric(options: ['mode' => ' loose'] )]
170
+ #[AcmeAssert\ContainsAlphanumeric(mode: ' loose')]
170
171
protected $name;
171
172
172
173
// ...
@@ -179,7 +180,8 @@ You can use custom validators like the ones provided by Symfony itself:
179
180
properties :
180
181
name :
181
182
- NotBlank : ~
182
- - App\Validator\ContainsAlphanumeric : ~
183
+ - App\Validator\ContainsAlphanumeric :
184
+ mode : ' loose'
183
185
184
186
.. code-block :: xml
185
187
@@ -192,7 +194,9 @@ You can use custom validators like the ones provided by Symfony itself:
192
194
<class name =" App\Entity\AcmeEntity" >
193
195
<property name =" name" >
194
196
<constraint name =" NotBlank" />
195
- <constraint name =" App\Validator\ContainsAlphanumeric" />
197
+ <constraint name =" App\Validator\ContainsAlphanumeric" >
198
+ <option name =" mode" >loose</option >
199
+ </constraint >
196
200
</property >
197
201
</class >
198
202
</constraint-mapping >
@@ -213,7 +217,7 @@ You can use custom validators like the ones provided by Symfony itself:
213
217
public static function loadValidatorMetadata(ClassMetadata $metadata)
214
218
{
215
219
$metadata->addPropertyConstraint('name', new NotBlank());
216
- $metadata->addPropertyConstraint('name', new ContainsAlphanumeric());
220
+ $metadata->addPropertyConstraint('name', new ContainsAlphanumeric(['mode' => 'loose'] ));
217
221
}
218
222
}
219
223
0 commit comments