Skip to content

Commit f2b6a68

Browse files
committed
minor #15997 [Validator] Add missing option to configs (sebpacz)
This PR was submitted for the 5.3 branch but it was merged into the 5.4 branch instead. Discussion ---------- [Validator] Add missing option to configs The `mode` option was defined in the `ContainsAlphanumeric` constraint and is used in several examples. I added it to all configuration examples. Commits ------- 13a2711 [Validator] Add missing option to configs
2 parents 8108407 + 13a2711 commit f2b6a68

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

validation/custom_constraint.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)