File tree Expand file tree Collapse file tree 4 files changed +106
-0
lines changed
Expand file tree Collapse file tree 4 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,26 @@ 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: the code of this class requires using PHP 8.1 or higher, because of the use of nested attributes
50+
51+ class User
52+ {
53+ #[
54+ Assert\All([
55+ new Assert\NotBlank,
56+ new Assert\Length(min: 5),
57+ ])
58+ ]
59+ protected $favoriteColors = [];
60+ }
61+
4262 .. code-block :: yaml
4363
4464 # config/validator/validation.yaml
Original file line number Diff line number Diff line change @@ -60,6 +60,36 @@ 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: the code of this class requires using PHP 8.1 or higher, because of the use of nested attributes
71+
72+ class Student
73+ {
74+ #[
75+ Assert\AtLeastOneOf([
76+ new Assert\Regex('/#/'),
77+ new Assert\Length(min: 10),
78+ ])
79+ ]
80+ protected $plainPassword;
81+
82+ #[
83+ Assert\AtLeastOneOf([
84+ new Assert\Count(min: 3),
85+ new Assert\All(
86+ new Assert\GreaterThanOrEqual(5)
87+ ),
88+ ])
89+ ]
90+ protected $grades;
91+ }
92+
6393 .. code-block :: yaml
6494
6595 # config/validator/validation.yaml
Original file line number Diff line number Diff line change @@ -88,6 +88,38 @@ 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: the code of this class requires using PHP 8.1 or higher, because of the use of nested attributes
99+
100+ class Author
101+ {
102+ #[
103+ Assert\Collection(
104+ fields: [
105+ 'personal_email' => new Assert\Email,
106+ 'short_bio' => [
107+ new Assert\NotBlank,
108+ new Assert\Length(
109+ max: 100,
110+ maxMessage: 'Your short bio is too long!'
111+ )
112+ ]
113+ ],
114+ allowMissingFields: true,
115+ )
116+ ]
117+ protected $profileData = [
118+ 'personal_email' => '...',
119+ 'short_bio' => '...',
120+ ];
121+ }
122+
91123 .. code-block :: yaml
92124
93125 # config/validator/validation.yaml
Original file line number Diff line number Diff line change @@ -67,6 +67,30 @@ 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: the code of this class requires using PHP 8.1 or higher, because of the use of nested attributes
79+
80+ class Place
81+ {
82+ #[
83+ Assert\Sequentially([
84+ new Assert\NotNull,
85+ new Assert\Type('string'),
86+ new Assert\Length(min: 10),
87+ new Assert\Regex(Place::ADDRESS_REGEX),
88+ new AcmeAssert\Geolocalizable,
89+ ])
90+ ]
91+ public $address;
92+ }
93+
7094 .. code-block :: yaml
7195
7296 # config/validator/validation.yaml
You can’t perform that action at this time.
0 commit comments