@@ -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
@@ -160,7 +161,7 @@ You can use custom validators like the ones provided by Symfony itself:
160
161
// ...
161
162
162
163
#[Assert\NotBlank]
163
- #[AcmeAssert\ContainsAlphanumeric(options: ['mode' => ' loose'] )]
164
+ #[AcmeAssert\ContainsAlphanumeric(mode: ' loose')]
164
165
protected $name;
165
166
166
167
// ...
@@ -173,7 +174,8 @@ You can use custom validators like the ones provided by Symfony itself:
173
174
properties :
174
175
name :
175
176
- NotBlank : ~
176
- - App\Validator\ContainsAlphanumeric : ~
177
+ - App\Validator\ContainsAlphanumeric :
178
+ mode : ' loose'
177
179
178
180
.. code-block :: xml
179
181
@@ -186,7 +188,9 @@ You can use custom validators like the ones provided by Symfony itself:
186
188
<class name =" App\Entity\AcmeEntity" >
187
189
<property name =" name" >
188
190
<constraint name =" NotBlank" />
189
- <constraint name =" App\Validator\ContainsAlphanumeric" />
191
+ <constraint name =" App\Validator\ContainsAlphanumeric" >
192
+ <option name =" mode" >loose</option >
193
+ </constraint >
190
194
</property >
191
195
</class >
192
196
</constraint-mapping >
@@ -207,7 +211,7 @@ You can use custom validators like the ones provided by Symfony itself:
207
211
public static function loadValidatorMetadata(ClassMetadata $metadata)
208
212
{
209
213
$metadata->addPropertyConstraint('name', new NotBlank());
210
- $metadata->addPropertyConstraint('name', new ContainsAlphanumeric());
214
+ $metadata->addPropertyConstraint('name', new ContainsAlphanumeric(['mode' => 'loose'] ));
211
215
}
212
216
}
213
217
0 commit comments