File tree 4 files changed +112
-0
lines changed
Filter options
4 files changed +112
-0
lines changed
Original file line number Diff line number Diff line change @@ -39,6 +39,23 @@ entry in that array:
39
39
protected $favoriteColors = [];
40
40
}
41
41
42
+ .. code-block :: php-attributes
43
+
44
+ // src/Entity/User.php
45
+ namespace App\Entity;
46
+
47
+ use Symfony\Component\Validator\Constraints as Assert;
48
+
49
+ // IMPORTANT: nested attributes requires PHP 8.1 or higher
50
+ class User
51
+ {
52
+ #[Assert\All([
53
+ new Assert\NotBlank,
54
+ new Assert\Length(min: 5),
55
+ ])]
56
+ protected $favoriteColors = [];
57
+ }
58
+
42
59
.. code-block :: yaml
43
60
44
61
# config/validator/validation.yaml
@@ -93,6 +110,11 @@ entry in that array:
93
110
}
94
111
}
95
112
113
+ .. versionadded :: 5.4
114
+
115
+ The ``#[All] `` PHP attribute was introduced in Symfony 5.4 and requires
116
+ PHP 8.1 (which added nested attribute support).
117
+
96
118
Now, each entry in the ``favoriteColors `` array will be validated to not
97
119
be blank and to be at least 5 characters long.
98
120
Original file line number Diff line number Diff line change @@ -60,6 +60,31 @@ The following constraints ensure that:
60
60
protected $grades;
61
61
}
62
62
63
+ .. code-block :: php-attributes
64
+
65
+ // src/Entity/Student.php
66
+ namespace App\Entity;
67
+
68
+ use Symfony\Component\Validator\Constraints as Assert;
69
+
70
+ // IMPORTANT: nested attributes requires PHP 8.1 or higher
71
+ class Student
72
+ {
73
+ #[Assert\AtLeastOneOf([
74
+ new Assert\Regex('/#/'),
75
+ new Assert\Length(min: 10),
76
+ ])]
77
+ protected $plainPassword;
78
+
79
+ #[Assert\AtLeastOneOf([
80
+ new Assert\Count(min: 3),
81
+ new Assert\All(
82
+ new Assert\GreaterThanOrEqual(5)
83
+ ),
84
+ ])]
85
+ protected $grades;
86
+ }
87
+
63
88
.. code-block :: yaml
64
89
65
90
# config/validator/validation.yaml
@@ -149,6 +174,11 @@ The following constraints ensure that:
149
174
}
150
175
}
151
176
177
+ .. versionadded :: 5.4
178
+
179
+ The ``#[AtLeastOneOf] `` PHP attribute was introduced in Symfony 5.4 and
180
+ requires PHP 8.1 (which added nested attribute support).
181
+
152
182
Options
153
183
-------
154
184
Original file line number Diff line number Diff line change @@ -88,6 +88,35 @@ following:
88
88
];
89
89
}
90
90
91
+ .. code-block :: php-attributes
92
+
93
+ // src/Entity/Author.php
94
+ namespace App\Entity;
95
+
96
+ use Symfony\Component\Validator\Constraints as Assert;
97
+
98
+ // IMPORTANT: nested attributes requires PHP 8.1 or higher
99
+ class Author
100
+ {
101
+ #[Assert\Collection(
102
+ fields: [
103
+ 'personal_email' => new Assert\Email,
104
+ 'short_bio' => [
105
+ new Assert\NotBlank,
106
+ new Assert\Length(
107
+ max: 100,
108
+ maxMessage: 'Your short bio is too long!'
109
+ )
110
+ ]
111
+ ],
112
+ allowMissingFields: true,
113
+ )]
114
+ protected $profileData = [
115
+ 'personal_email' => '...',
116
+ 'short_bio' => '...',
117
+ ];
118
+ }
119
+
91
120
.. code-block :: yaml
92
121
93
122
# config/validator/validation.yaml
@@ -162,6 +191,11 @@ following:
162
191
}
163
192
}
164
193
194
+ .. versionadded :: 5.4
195
+
196
+ The ``#[Collection] `` PHP attribute was introduced in Symfony 5.4 and
197
+ requires PHP 8.1 (which added nested attribute support).
198
+
165
199
Presence and Absence of Fields
166
200
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167
201
Original file line number Diff line number Diff line change @@ -67,6 +67,27 @@ You can validate each of these constraints sequentially to solve these issues:
67
67
public $address;
68
68
}
69
69
70
+ .. code-block :: php-attributes
71
+
72
+ // src/Localization/Place.php
73
+ namespace App\Localization;
74
+
75
+ use App\Validator\Constraints as AcmeAssert;
76
+ use Symfony\Component\Validator\Constraints as Assert;
77
+
78
+ // IMPORTANT: nested attributes requires PHP 8.1 or higher
79
+ class Place
80
+ {
81
+ #[Assert\Sequentially([
82
+ new Assert\NotNull,
83
+ new Assert\Type('string'),
84
+ new Assert\Length(min: 10),
85
+ new Assert\Regex(Place::ADDRESS_REGEX),
86
+ new AcmeAssert\Geolocalizable,
87
+ ])]
88
+ public $address;
89
+ }
90
+
70
91
.. code-block :: yaml
71
92
72
93
# config/validator/validation.yaml
@@ -128,6 +149,11 @@ You can validate each of these constraints sequentially to solve these issues:
128
149
}
129
150
}
130
151
152
+ .. versionadded :: 5.4
153
+
154
+ The ``#[Sequentially] `` PHP attribute was introduced in Symfony 5.4 and
155
+ requires PHP 8.1 (which added nested attribute support).
156
+
131
157
Options
132
158
-------
133
159
You can’t perform that action at this time.
0 commit comments