Skip to content

Commit 4e31dd9

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: [Validator] Add PHP Attributes example to Compound
2 parents 2c08253 + b920cfd commit 4e31dd9

File tree

1 file changed

+68
-19
lines changed

1 file changed

+68
-19
lines changed

reference/constraints/Compound.rst

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,63 @@ Basic Usage
2323
Suppose that you have different places where a user password must be validated,
2424
you can create your own named set or requirements to be reused consistently everywhere::
2525

26-
// src/Validator/Constraints/PasswordRequirements.php
27-
namespace App\Validator\Constraints;
28-
29-
use Symfony\Component\Validator\Constraints\Compound;
30-
use Symfony\Component\Validator\Constraints as Assert;
31-
32-
/**
33-
* @Annotation
34-
*/
35-
class PasswordRequirements extends Compound
36-
{
37-
protected function getConstraints(array $options): array
26+
.. configuration-block::
27+
28+
.. code-block:: php-annotations
29+
30+
// src/Validator/Constraints/PasswordRequirements.php
31+
namespace App\Validator\Constraints;
32+
33+
use Symfony\Component\Validator\Constraints\Compound;
34+
use Symfony\Component\Validator\Constraints as Assert;
35+
36+
/**
37+
* @Annotation
38+
*/
39+
class PasswordRequirements extends Compound
3840
{
39-
return [
40-
new Assert\NotBlank(),
41-
new Assert\Type('string'),
42-
new Assert\Length(['min' => 12]),
43-
new Assert\NotCompromisedPassword(),
44-
];
41+
protected function getConstraints(array $options): array
42+
{
43+
return [
44+
new Assert\NotBlank(),
45+
new Assert\Type('string'),
46+
new Assert\Length(['min' => 12]),
47+
new Assert\NotCompromisedPassword(),
48+
];
49+
}
4550
}
46-
}
51+
52+
.. code-block:: php-attributes
53+
54+
// src/Validator/Constraints/PasswordRequirements.php
55+
namespace App\Validator\Constraints;
56+
57+
use Symfony\Component\Validator\Constraints\Compound;
58+
use Symfony\Component\Validator\Constraints as Assert;
59+
60+
#[\Attribute]
61+
class PasswordRequirements extends Compound
62+
{
63+
protected function getConstraints(array $options): array
64+
{
65+
return [
66+
new Assert\NotBlank(),
67+
new Assert\Type('string'),
68+
new Assert\Length(['min' => 12]),
69+
new Assert\NotCompromisedPassword(),
70+
];
71+
}
72+
}
73+
74+
Add ``@Annotation`` or ``#[\Attribute]`` to the constraint class if you want to
75+
use it as an annotation/attribute in other classes. If the constraint has
76+
configuration options, define them as public properties on the constraint class.
77+
78+
.. versionadded:: 5.2
79+
80+
The ability to use PHP attributes to configure constraints was introduced in
81+
Symfony 5.2. Prior to this, Doctrine Annotations were the only way to
82+
annotate constraints.
4783

4884
You can now use it anywhere you need it:
4985

@@ -64,6 +100,19 @@ You can now use it anywhere you need it:
64100
public $password;
65101
}
66102
103+
.. code-block:: php-attributes
104+
105+
// src/User/RegisterUser.php
106+
namespace App\User;
107+
108+
use App\Validator\Constraints as AcmeAssert;
109+
110+
class RegisterUser
111+
{
112+
#[AcmeAssert\PasswordRequirements]
113+
public $password;
114+
}
115+
67116
.. code-block:: yaml
68117
69118
# config/validator/validation.yaml

0 commit comments

Comments
 (0)