Skip to content

Commit 07a39ed

Browse files
[Validator] Add attributes documentation of composite constraints
1 parent 6144e6a commit 07a39ed

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

reference/constraints/All.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ 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+
class User
50+
{
51+
#[
52+
Assert\All([
53+
new Assert\NotBlank,
54+
new Assert\Length(min: 5),
55+
])
56+
]
57+
protected $favoriteColors = [];
58+
}
59+
4260
.. code-block:: yaml
4361
4462
# config/validator/validation.yaml

reference/constraints/AtLeastOneOf.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ 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+
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+
6391
.. code-block:: yaml
6492
6593
# config/validator/validation.yaml

reference/constraints/Collection.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@ 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+
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+
91121
.. code-block:: yaml
92122
93123
# config/validator/validation.yaml

reference/constraints/Sequentially.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ 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+
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+
7092
.. code-block:: yaml
7193
7294
# config/validator/validation.yaml

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy