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:
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: 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
+
42
62
.. code-block :: yaml
43
63
44
64
# config/validator/validation.yaml
Original file line number Diff line number Diff line change @@ -60,6 +60,36 @@ 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: 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
+
63
93
.. code-block :: yaml
64
94
65
95
# config/validator/validation.yaml
Original file line number Diff line number Diff line change @@ -88,6 +88,38 @@ 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: 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
+
91
123
.. code-block :: yaml
92
124
93
125
# 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:
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: 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
+
70
94
.. code-block :: yaml
71
95
72
96
# config/validator/validation.yaml
You can’t perform that action at this time.
0 commit comments