Skip to content

Commit 152097e

Browse files
committed
use named arguments to configure validation constraints
1 parent 0c031e7 commit 152097e

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

reference/constraints/Collection.rst

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ following:
144144
'personal_email' => new Assert\Email(),
145145
'short_bio' => [
146146
new Assert\NotBlank(),
147-
new Assert\Length([
148-
'max' => 100,
149-
'maxMessage' => 'Your short bio is too long!',
150-
]),
147+
new Assert\Length(
148+
max: 100,
149+
maxMessage: 'Your short bio is too long!',
150+
),
151151
],
152152
],
153153
allowMissingFields: true,
@@ -196,13 +196,13 @@ you can do the following:
196196
{
197197
#[Assert\Collection(
198198
fields: [
199-
'personal_email' => new Assert\Required([
199+
'personal_email' => new Assert\Required(constraints: [
200200
new Assert\NotBlank,
201201
new Assert\Email,
202202
]),
203-
'alternate_email' => new Assert\Optional(
203+
'alternate_email' => new Assert\Optional(constraints: [
204204
new Assert\Email
205-
),
205+
]),
206206
],
207207
)]
208208
protected array $profileData = ['personal_email' => 'email@example.com'];
@@ -218,11 +218,13 @@ you can do the following:
218218
fields:
219219
personal_email:
220220
- Required:
221-
- NotBlank: ~
222-
- Email: ~
221+
constraints:
222+
- NotBlank: ~
223+
- Email: ~
223224
alternate_email:
224225
- Optional:
225-
- Email: ~
226+
constraints:
227+
- Email: ~
226228
227229
.. code-block:: xml
228230
@@ -238,13 +240,17 @@ you can do the following:
238240
<option name="fields">
239241
<value key="personal_email">
240242
<constraint name="Required">
241-
<constraint name="NotBlank"/>
242-
<constraint name="Email"/>
243+
<option name="constraints">
244+
<constraint name="NotBlank"/>
245+
<constraint name="Email"/>
246+
</option>
243247
</constraint>
244248
</value>
245249
<value key="alternate_email">
246250
<constraint name="Optional">
247-
<constraint name="Email"/>
251+
<option name="constraints">
252+
<constraint name="Email"/>
253+
</option>
248254
</constraint>
249255
</value>
250256
</option>
@@ -269,11 +275,11 @@ you can do the following:
269275
{
270276
$metadata->addPropertyConstraint('profileData', new Assert\Collection(
271277
fields: [
272-
'personal_email' => new Assert\Required([
278+
'personal_email' => new Assert\Required(constraints: [
273279
new Assert\NotBlank(),
274280
new Assert\Email(),
275281
]),
276-
'alternate_email' => new Assert\Optional(new Assert\Email()),
282+
'alternate_email' => new Assert\Optional(constraints: [new Assert\Email()]),
277283
],
278284
));
279285
}
@@ -293,8 +299,8 @@ groups. Take the following example::
293299

294300
$constraint = new Assert\Collection(
295301
fields: [
296-
'name' => new Assert\NotBlank(['groups' => 'basic']),
297-
'email' => new Assert\NotBlank(['groups' => 'contact']),
302+
'name' => new Assert\NotBlank(groups: ['basic']),
303+
'email' => new Assert\NotBlank(groups: ['contact']),
298304
],
299305
);
300306

validation/raw_values.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,22 @@ Validation of arrays is possible using the ``Collection`` constraint::
6767

6868
$groups = new Assert\GroupSequence(['Default', 'custom']);
6969

70-
$constraint = new Assert\Collection([
70+
$constraint = new Assert\Collection(fields: [
7171
// the keys correspond to the keys in the input array
72-
'name' => new Assert\Collection([
73-
'first_name' => new Assert\Length(['min' => 101]),
74-
'last_name' => new Assert\Length(['min' => 1]),
72+
'name' => new Assert\Collection(fields: [
73+
'first_name' => new Assert\Length(min: 101),
74+
'last_name' => new Assert\Length(min: 1),
7575
]),
7676
'email' => new Assert\Email(),
77-
'simple' => new Assert\Length(['min' => 102]),
77+
'simple' => new Assert\Length(min: 102),
7878
'eye_color' => new Assert\Choice(choices: [3, 4]),
7979
'file' => new Assert\File(),
80-
'password' => new Assert\Length(['min' => 60]),
81-
'tags' => new Assert\Optional([
82-
new Assert\Type('array'),
83-
new Assert\Count(['min' => 1]),
80+
'password' => new Assert\Length(min: 60),
81+
'tags' => new Assert\Optional(constraints: [
82+
new Assert\Type(type: 'array'),
83+
new Assert\Count(min: 1),
8484
new Assert\All([
85-
new Assert\Collection([
85+
new Assert\Collection(constraints: [
8686
'slug' => [
8787
new Assert\NotBlank(),
8888
new Assert\Type(['type' => 'string']),
@@ -91,7 +91,7 @@ Validation of arrays is possible using the ``Collection`` constraint::
9191
new Assert\NotBlank(),
9292
],
9393
]),
94-
new CustomUniqueTagValidator(['groups' => 'custom']),
94+
new CustomUniqueTagValidator(groups: ['custom']),
9595
]),
9696
]),
9797
]);

0 commit comments

Comments
 (0)