File tree Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,24 @@ 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
+ class User
50
+ {
51
+ #[
52
+ Assert\All([
53
+ new Assert\NotBlank,
54
+ new Assert\Length(min: 5),
55
+ ])
56
+ ]
57
+ protected $favoriteColors = [];
58
+ }
59
+
42
60
.. code-block :: yaml
43
61
44
62
# config/validator/validation.yaml
Original file line number Diff line number Diff line change @@ -60,6 +60,34 @@ 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
+ class Student
71
+ {
72
+ #[
73
+ Assert\AtLeastOneOf([
74
+ new Assert\Regex('/#/'),
75
+ new Assert\Length(min: 10),
76
+ ])
77
+ ]
78
+ protected $password;
79
+
80
+ #[
81
+ Assert\AtLeastOneOf([
82
+ new Assert\Count(min: 3),
83
+ new Assert\All(
84
+ new Assert\GreaterThanOrEqual(5)
85
+ ),
86
+ ])
87
+ ]
88
+ protected $grades;
89
+ }
90
+
63
91
.. code-block :: yaml
64
92
65
93
# config/validator/validation.yaml
Original file line number Diff line number Diff line change @@ -88,6 +88,36 @@ 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
+ class Author
99
+ {
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
+ ]
115
+ protected $profileData = [
116
+ 'personal_email' => '...',
117
+ 'short_bio' => '...',
118
+ ];
119
+ }
120
+
91
121
.. code-block :: yaml
92
122
93
123
# config/validator/validation.yaml
Original file line number Diff line number Diff line change @@ -67,6 +67,28 @@ 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
+ class Place
79
+ {
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
+ ]
89
+ public $address;
90
+ }
91
+
70
92
.. code-block :: yaml
71
93
72
94
# config/validator/validation.yaml
You can’t perform that action at this time.
0 commit comments