Skip to content

Commit 80cd5b8

Browse files
committed
minor #14865 [Validator] Use PHP attributes when creating custom validation constraints (yoannrenard)
This PR was squashed before being merged into the 5.2 branch. Discussion ---------- [Validator] Use PHP attributes when creating custom validation constraints Describe how to use PHP attributes when creating custom validation constraints Commits ------- 55c5676 [Validator] Use PHP attributes when creating custom validation constraints
2 parents 188c186 + 55c5676 commit 80cd5b8

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed

validation/custom_constraint.rst

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,43 @@ alphanumeric characters.
1212
Creating the Constraint Class
1313
-----------------------------
1414

15-
First you need to create a Constraint class and extend :class:`Symfony\\Component\\Validator\\Constraint`::
15+
First you need to create a Constraint class and extend :class:`Symfony\\Component\\Validator\\Constraint`:
1616

17-
// src/Validator/ContainsAlphanumeric.php
18-
namespace App\Validator;
17+
.. configuration-block::
1918

20-
use Symfony\Component\Validator\Constraint;
19+
.. code-block:: php-annotations
2120
22-
/**
23-
* @Annotation
24-
*/
25-
class ContainsAlphanumeric extends Constraint
26-
{
27-
public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
28-
}
21+
// src/Validator/ContainsAlphanumeric.php
22+
namespace App\Validator;
23+
24+
use Symfony\Component\Validator\Constraint;
25+
26+
/**
27+
* @Annotation
28+
*/
29+
class ContainsAlphanumeric extends Constraint
30+
{
31+
public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
32+
}
33+
34+
.. code-block:: php-attributes
35+
36+
// src/Validator/ContainsAlphanumeric.php
37+
namespace App\Validator;
38+
39+
use Symfony\Component\Validator\Constraint;
40+
41+
#[\Attribute]
42+
class ContainsAlphanumeric extends Constraint
43+
{
44+
public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
45+
}
46+
47+
.. versionadded:: 5.2
48+
49+
The ability to use PHP attributes to configure constraints was introduced in
50+
Symfony 5.2. Prior to this, Doctrine Annotations were the only way to
51+
annotate constraints.
2952

3053
.. note::
3154

@@ -128,6 +151,25 @@ You can use custom validators like the ones provided by Symfony itself:
128151
// ...
129152
}
130153
154+
.. code-block:: php-attributes
155+
156+
// src/Entity/AcmeEntity.php
157+
namespace App\Entity;
158+
159+
use App\Validator as AcmeAssert;
160+
use Symfony\Component\Validator\Constraints as Assert;
161+
162+
class AcmeEntity
163+
{
164+
// ...
165+
166+
#[Assert\NotBlank]
167+
#[AcmeAssert\ContainsAlphanumeric]
168+
protected $name;
169+
170+
// ...
171+
}
172+
131173
.. code-block:: yaml
132174
133175
# config/validator/validation.yaml
@@ -241,6 +283,14 @@ not to the property:
241283
// ...
242284
}
243285
286+
.. code-block:: php-attributes
287+
288+
#[AcmeAssert\ProtocolClass]
289+
class AcmeEntity
290+
{
291+
// ...
292+
}
293+
244294
.. code-block:: yaml
245295
246296
# config/validator/validation.yaml

0 commit comments

Comments
 (0)