File tree Expand file tree Collapse file tree 4 files changed +112
-0
lines changed Expand file tree Collapse file tree 4 files changed +112
-0
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,23 @@ entry in that array:
36
36
protected $favoriteColors = [];
37
37
}
38
38
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
+
39
56
.. code-block :: yaml
40
57
41
58
# config/validator/validation.yaml
@@ -90,6 +107,11 @@ entry in that array:
90
107
}
91
108
}
92
109
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
+
93
115
Now, each entry in the ``favoriteColors `` array will be validated to not
94
116
be blank and to be at least 5 characters long.
95
117
Original file line number Diff line number Diff line change @@ -54,6 +54,31 @@ The following constraints ensure that:
54
54
protected $grades;
55
55
}
56
56
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
+
57
82
.. code-block :: yaml
58
83
59
84
# config/validator/validation.yaml
@@ -143,6 +168,11 @@ The following constraints ensure that:
143
168
}
144
169
}
145
170
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
+
146
176
Options
147
177
-------
148
178
Original file line number Diff line number Diff line change @@ -81,6 +81,35 @@ following:
81
81
];
82
82
}
83
83
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
+
84
113
.. code-block :: yaml
85
114
86
115
# config/validator/validation.yaml
@@ -155,6 +184,11 @@ following:
155
184
}
156
185
}
157
186
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
+
158
192
Presence and Absence of Fields
159
193
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160
194
Original file line number Diff line number Diff line change @@ -64,6 +64,27 @@ You can validate each of these constraints sequentially to solve these issues:
64
64
public $address;
65
65
}
66
66
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
+
67
88
.. code-block :: yaml
68
89
69
90
# config/validator/validation.yaml
@@ -125,6 +146,11 @@ You can validate each of these constraints sequentially to solve these issues:
125
146
}
126
147
}
127
148
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
+
128
154
Options
129
155
-------
130
156
You can’t perform that action at this time.
0 commit comments