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 @@ -39,6 +39,23 @@ entry in that array:
3939 protected $favoriteColors = [];
4040 }
4141
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+
4259 .. code-block :: yaml
4360
4461 # config/validator/validation.yaml
@@ -93,6 +110,11 @@ entry in that array:
93110 }
94111 }
95112
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+
96118Now, each entry in the ``favoriteColors `` array will be validated to not
97119be blank and to be at least 5 characters long.
98120
Original file line number Diff line number Diff line change @@ -60,6 +60,31 @@ The following constraints ensure that:
6060 protected $grades;
6161 }
6262
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+
6388 .. code-block :: yaml
6489
6590 # config/validator/validation.yaml
@@ -149,6 +174,11 @@ The following constraints ensure that:
149174 }
150175 }
151176
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+
152182Options
153183-------
154184
Original file line number Diff line number Diff line change @@ -88,6 +88,35 @@ following:
8888 ];
8989 }
9090
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+
91120 .. code-block :: yaml
92121
93122 # config/validator/validation.yaml
@@ -162,6 +191,11 @@ following:
162191 }
163192 }
164193
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+
165199Presence and Absence of Fields
166200~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167201
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:
6767 public $address;
6868 }
6969
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+
7091 .. code-block :: yaml
7192
7293 # config/validator/validation.yaml
@@ -128,6 +149,11 @@ You can validate each of these constraints sequentially to solve these issues:
128149 }
129150 }
130151
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+
131157Options
132158-------
133159
You can’t perform that action at this time.
0 commit comments