Skip to content

Commit 441222b

Browse files
committed
feature #15541 [Validator] Add attributes documentation of composite constraints (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [Validator] Add attributes documentation of composite constraints Related pull-request: symfony/symfony#41994 Not really sure how to precise it only works for attributes in PHP 8.1, not 8.0. If you have a solution, please let me know! Commits ------- 7099e3f [Validator] Add attributes documentation of composite constraints
2 parents 12fdcc3 + 7099e3f commit 441222b

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

reference/constraints/All.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ entry in that array:
3636
protected $favoriteColors = [];
3737
}
3838
39+
.. code-block:: php-attributes
40+
41+
// src/Entity/User.php
42+
namespace App\Entity;
43+
44+
use Symfony\Component\Validator\Constraints as Assert;
45+
46+
// IMPORTANT: nested attributes requires PHP 8.1 or higher
47+
class User
48+
{
49+
#[Assert\All([
50+
new Assert\NotBlank,
51+
new Assert\Length(min: 5),
52+
])]
53+
protected $favoriteColors = [];
54+
}
55+
3956
.. code-block:: yaml
4057
4158
# config/validator/validation.yaml
@@ -90,6 +107,11 @@ entry in that array:
90107
}
91108
}
92109
110+
.. versionadded:: 5.4
111+
112+
The ``#[All]`` PHP attribute was introduced in Symfony 5.4 and requires
113+
PHP 8.1 (which added nested attribute support).
114+
93115
Now, each entry in the ``favoriteColors`` array will be validated to not
94116
be blank and to be at least 5 characters long.
95117

reference/constraints/AtLeastOneOf.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ The following constraints ensure that:
5454
protected $grades;
5555
}
5656
57+
.. code-block:: php-attributes
58+
59+
// src/Entity/Student.php
60+
namespace App\Entity;
61+
62+
use Symfony\Component\Validator\Constraints as Assert;
63+
64+
// IMPORTANT: nested attributes requires PHP 8.1 or higher
65+
class Student
66+
{
67+
#[Assert\AtLeastOneOf([
68+
new Assert\Regex('/#/'),
69+
new Assert\Length(min: 10),
70+
])]
71+
protected $plainPassword;
72+
73+
#[Assert\AtLeastOneOf([
74+
new Assert\Count(min: 3),
75+
new Assert\All(
76+
new Assert\GreaterThanOrEqual(5)
77+
),
78+
])]
79+
protected $grades;
80+
}
81+
5782
.. code-block:: yaml
5883
5984
# config/validator/validation.yaml
@@ -143,6 +168,11 @@ The following constraints ensure that:
143168
}
144169
}
145170
171+
.. versionadded:: 5.4
172+
173+
The ``#[AtLeastOneOf]`` PHP attribute was introduced in Symfony 5.4 and
174+
requires PHP 8.1 (which added nested attribute support).
175+
146176
Options
147177
-------
148178

reference/constraints/Collection.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,35 @@ following:
8181
];
8282
}
8383
84+
.. code-block:: php-attributes
85+
86+
// src/Entity/Author.php
87+
namespace App\Entity;
88+
89+
use Symfony\Component\Validator\Constraints as Assert;
90+
91+
// IMPORTANT: nested attributes requires PHP 8.1 or higher
92+
class Author
93+
{
94+
#[Assert\Collection(
95+
fields: [
96+
'personal_email' => new Assert\Email,
97+
'short_bio' => [
98+
new Assert\NotBlank,
99+
new Assert\Length(
100+
max: 100,
101+
maxMessage: 'Your short bio is too long!'
102+
)
103+
]
104+
],
105+
allowMissingFields: true,
106+
)]
107+
protected $profileData = [
108+
'personal_email' => '...',
109+
'short_bio' => '...',
110+
];
111+
}
112+
84113
.. code-block:: yaml
85114
86115
# config/validator/validation.yaml
@@ -155,6 +184,11 @@ following:
155184
}
156185
}
157186
187+
.. versionadded:: 5.4
188+
189+
The ``#[Collection]`` PHP attribute was introduced in Symfony 5.4 and
190+
requires PHP 8.1 (which added nested attribute support).
191+
158192
Presence and Absence of Fields
159193
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160194

reference/constraints/Sequentially.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ You can validate each of these constraints sequentially to solve these issues:
6464
public $address;
6565
}
6666
67+
.. code-block:: php-attributes
68+
69+
// src/Localization/Place.php
70+
namespace App\Localization;
71+
72+
use App\Validator\Constraints as AcmeAssert;
73+
use Symfony\Component\Validator\Constraints as Assert;
74+
75+
// IMPORTANT: nested attributes requires PHP 8.1 or higher
76+
class Place
77+
{
78+
#[Assert\Sequentially([
79+
new Assert\NotNull,
80+
new Assert\Type('string'),
81+
new Assert\Length(min: 10),
82+
new Assert\Regex(Place::ADDRESS_REGEX),
83+
new AcmeAssert\Geolocalizable,
84+
])]
85+
public $address;
86+
}
87+
6788
.. code-block:: yaml
6889
6990
# config/validator/validation.yaml
@@ -125,6 +146,11 @@ You can validate each of these constraints sequentially to solve these issues:
125146
}
126147
}
127148
149+
.. versionadded:: 5.4
150+
151+
The ``#[Sequentially]`` PHP attribute was introduced in Symfony 5.4 and
152+
requires PHP 8.1 (which added nested attribute support).
153+
128154
Options
129155
-------
130156

0 commit comments

Comments
 (0)