Skip to content

Commit a00dddb

Browse files
committed
use named-arguments to configure validation constraint options
1 parent 171af72 commit a00dddb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+343
-427
lines changed

Diff for: components/console/helpers/questionhelper.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,10 @@ invalid answer and will only be able to proceed if their input is valid.
480480
use Symfony\Component\Validator\Validation;
481481

482482
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
483-
$validation = Validation::createCallable(new Regex([
484-
'pattern' => '/^[a-zA-Z]+Bundle$/',
485-
'message' => 'The name of the bundle should be suffixed with \'Bundle\'',
486-
]));
483+
$validation = Validation::createCallable(new Regex(
484+
pattern: '/^[a-zA-Z]+Bundle$/',
485+
message: 'The name of the bundle should be suffixed with \'Bundle\'',
486+
));
487487
$question->setValidator($validation);
488488

489489
Validating a Hidden Response

Diff for: components/options_resolver.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ returns ``true`` for acceptable values and ``false`` for invalid values::
394394

395395
// ...
396396
$resolver->setAllowedValues('transport', Validation::createIsValidCallable(
397-
new Length(['min' => 10 ])
397+
new Length(min: 10)
398398
));
399399

400400
In sub-classes, you can use :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedValues`

Diff for: components/validator.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ characters long::
3636

3737
$validator = Validation::createValidator();
3838
$violations = $validator->validate('Bernhard', [
39-
new Length(['min' => 10]),
39+
new Length(min: 10),
4040
new NotBlank(),
4141
]);
4242

Diff for: components/validator/metadata.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ the ``Author`` class has at least 3 characters::
2424
$metadata->addPropertyConstraint('firstName', new Assert\NotBlank());
2525
$metadata->addPropertyConstraint(
2626
'firstName',
27-
new Assert\Length(["min" => 3])
27+
new Assert\Length(min: 3)
2828
);
2929
}
3030
}
@@ -55,9 +55,9 @@ Then, add the Validator component configuration to the class::
5555
{
5656
public static function loadValidatorMetadata(ClassMetadata $metadata): void
5757
{
58-
$metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue([
59-
'message' => 'The password cannot match your first name',
60-
]));
58+
$metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue(
59+
message: 'The password cannot match your first name',
60+
));
6161
}
6262
}
6363

Diff for: components/validator/resources.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ In this example, the validation metadata is retrieved executing the
4242
public static function loadValidatorMetadata(ClassMetadata $metadata): void
4343
{
4444
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
45-
$metadata->addPropertyConstraint('name', new Assert\Length([
46-
'min' => 5,
47-
'max' => 20,
48-
]));
45+
$metadata->addPropertyConstraint('name', new Assert\Length(
46+
min: 5,
47+
max: 20,
48+
));
4949
}
5050
}
5151

Diff for: controller/upload_file.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ so Symfony doesn't try to get/set its value from the related entity::
7575
// unmapped fields can't define their validation using attributes
7676
// in the associated entity, so you can use the PHP constraint classes
7777
'constraints' => [
78-
new File([
79-
'maxSize' => '1024k',
80-
'mimeTypes' => [
78+
new File(
79+
maxSize: '1024k',
80+
mimeTypes: [
8181
'application/pdf',
8282
'application/x-pdf',
8383
],
84-
'mimeTypesMessage' => 'Please upload a valid PDF document',
85-
])
84+
mimeTypesMessage: 'Please upload a valid PDF document',
85+
)
8686
],
8787
])
8888
// ...

Diff for: form/without_class.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ but here's a short example::
9696
{
9797
$builder
9898
->add('firstName', TextType::class, [
99-
'constraints' => new Length(['min' => 3]),
99+
'constraints' => new Length(min: 3),
100100
])
101101
->add('lastName', TextType::class, [
102102
'constraints' => [
103103
new NotBlank(),
104-
new Length(['min' => 3]),
104+
new Length(min: 3),
105105
],
106106
])
107107
;
@@ -153,10 +153,10 @@ This can be done by setting the ``constraints`` option in the
153153
$resolver->setDefaults([
154154
'data_class' => null,
155155
'constraints' => new Collection([
156-
'firstName' => new Length(['min' => 3]),
156+
'firstName' => new Length(min: 3),
157157
'lastName' => [
158158
new NotBlank(),
159-
new Length(['min' => 3]),
159+
new Length(min: 3),
160160
],
161161
]),
162162
]);

Diff for: reference/constraints/All.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ entry in that array:
7979
{
8080
public static function loadValidatorMetadata(ClassMetadata $metadata): void
8181
{
82-
$metadata->addPropertyConstraint('favoriteColors', new Assert\All([
83-
'constraints' => [
82+
$metadata->addPropertyConstraint('favoriteColors', new Assert\All(
83+
constraints: [
8484
new Assert\NotBlank(),
85-
new Assert\Length(['min' => 5]),
85+
new Assert\Length(min: 5),
8686
],
87-
]));
87+
));
8888
}
8989
}
9090
@@ -97,7 +97,7 @@ Options
9797
``constraints``
9898
~~~~~~~~~~~~~~~
9999

100-
**type**: ``array`` [:ref:`default option <validation-default-option>`]
100+
**type**: ``array``
101101

102102
This required option is the array of validation constraints that you want
103103
to apply to each element of the underlying array.

Diff for: reference/constraints/AtLeastOneOf.rst

+13-13
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,23 @@ The following constraints ensure that:
115115
{
116116
public static function loadValidatorMetadata(ClassMetadata $metadata): void
117117
{
118-
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([
119-
'constraints' => [
120-
new Assert\Regex(['pattern' => '/#/']),
121-
new Assert\Length(['min' => 10]),
118+
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf(
119+
constraints: [
120+
new Assert\Regex(pattern: '/#/'),
121+
new Assert\Length(min: 10),
122122
],
123-
]));
123+
));
124124
125-
$metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf([
126-
'constraints' => [
127-
new Assert\Count(['min' => 3]),
128-
new Assert\All([
129-
'constraints' => [
125+
$metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf(
126+
constraints: [
127+
new Assert\Count(min: 3),
128+
new Assert\All(
129+
constraints: [
130130
new Assert\GreaterThanOrEqual(5),
131131
],
132-
]),
132+
),
133133
],
134-
]));
134+
));
135135
}
136136
}
137137
@@ -141,7 +141,7 @@ Options
141141
constraints
142142
~~~~~~~~~~~
143143

144-
**type**: ``array`` [:ref:`default option <validation-default-option>`]
144+
**type**: ``array``
145145

146146
This required option is the array of validation constraints from which at least one of
147147
has to be satisfied in order for the validation to succeed.

Diff for: reference/constraints/Callback.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Options
259259
``callback``
260260
~~~~~~~~~~~~
261261

262-
**type**: ``string``, ``array`` or ``Closure`` [:ref:`default option <validation-default-option>`]
262+
**type**: ``string``, ``array`` or ``Closure``
263263

264264
The callback option accepts three different formats for specifying the
265265
callback method:

Diff for: reference/constraints/CardScheme.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ on an object that will contain a credit card number.
7777
{
7878
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7979
{
80-
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme([
81-
'schemes' => [
80+
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme(
81+
schemes: [
8282
Assert\CardScheme::VISA,
8383
],
84-
'message' => 'Your credit card number is invalid.',
85-
]));
84+
message: 'Your credit card number is invalid.',
85+
));
8686
}
8787
}
8888
@@ -114,7 +114,7 @@ Parameter Description
114114
``schemes``
115115
~~~~~~~~~~~
116116

117-
**type**: ``mixed`` [:ref:`default option <validation-default-option>`]
117+
**type**: ``mixed``
118118

119119
This option is required and represents the name of the number scheme used
120120
to validate the credit card number, it can either be a string or an array.

Diff for: reference/constraints/Choice.rst

+11-11
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ If your valid choice list is simple, you can pass them in directly via the
100100
new Assert\Choice(['New York', 'Berlin', 'Tokyo'])
101101
);
102102
103-
$metadata->addPropertyConstraint('genre', new Assert\Choice([
104-
'choices' => ['fiction', 'non-fiction'],
105-
'message' => 'Choose a valid genre.',
106-
]));
103+
$metadata->addPropertyConstraint('genre', new Assert\Choice(
104+
choices: ['fiction', 'non-fiction'],
105+
message: 'Choose a valid genre.',
106+
));
107107
}
108108
}
109109
@@ -182,9 +182,9 @@ constraint.
182182
183183
public static function loadValidatorMetadata(ClassMetadata $metadata): void
184184
{
185-
$metadata->addPropertyConstraint('genre', new Assert\Choice([
186-
'callback' => 'getGenres',
187-
]));
185+
$metadata->addPropertyConstraint('genre', new Assert\Choice(
186+
callback: 'getGenres',
187+
));
188188
}
189189
}
190190
@@ -250,9 +250,9 @@ you can pass the class name and the method as an array.
250250
251251
public static function loadValidatorMetadata(ClassMetadata $metadata): void
252252
{
253-
$metadata->addPropertyConstraint('genre', new Assert\Choice([
254-
'callback' => [Genre::class, 'getGenres'],
255-
]));
253+
$metadata->addPropertyConstraint('genre', new Assert\Choice(
254+
callback: [Genre::class, 'getGenres'],
255+
));
256256
}
257257
}
258258
@@ -271,7 +271,7 @@ to return the choices array. See
271271
``choices``
272272
~~~~~~~~~~~
273273

274-
**type**: ``array`` [:ref:`default option <validation-default-option>`]
274+
**type**: ``array``
275275

276276
A required option (unless `callback`_ is specified) - this is the array
277277
of options that should be considered in the valid set. The input value

Diff for: reference/constraints/Collection.rst

+23-23
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ following:
139139
140140
public static function loadValidatorMetadata(ClassMetadata $metadata): void
141141
{
142-
$metadata->addPropertyConstraint('profileData', new Assert\Collection([
143-
'fields' => [
142+
$metadata->addPropertyConstraint('profileData', new Assert\Collection(
143+
fields: [
144144
'personal_email' => new Assert\Email(),
145145
'short_bio' => [
146146
new Assert\NotBlank(),
@@ -150,8 +150,8 @@ following:
150150
]),
151151
],
152152
],
153-
'allowMissingFields' => true,
154-
]));
153+
allowMissingFields: true,
154+
));
155155
}
156156
}
157157
@@ -267,15 +267,15 @@ you can do the following:
267267
268268
public static function loadValidatorMetadata(ClassMetadata $metadata): void
269269
{
270-
$metadata->addPropertyConstraint('profileData', new Assert\Collection([
271-
'fields' => [
270+
$metadata->addPropertyConstraint('profileData', new Assert\Collection(
271+
fields: [
272272
'personal_email' => new Assert\Required([
273273
new Assert\NotBlank(),
274274
new Assert\Email(),
275275
]),
276276
'alternate_email' => new Assert\Optional(new Assert\Email()),
277277
],
278-
]));
278+
));
279279
}
280280
}
281281
@@ -291,28 +291,28 @@ groups. Take the following example::
291291

292292
use Symfony\Component\Validator\Constraints as Assert;
293293

294-
$constraint = new Assert\Collection([
295-
'fields' => [
294+
$constraint = new Assert\Collection(
295+
fields: [
296296
'name' => new Assert\NotBlank(['groups' => 'basic']),
297297
'email' => new Assert\NotBlank(['groups' => 'contact']),
298298
],
299-
]);
299+
);
300300

301301
This will result in the following configuration::
302302

303-
$constraint = new Assert\Collection([
304-
'fields' => [
305-
'name' => new Assert\Required([
306-
'constraints' => new Assert\NotBlank(['groups' => 'basic']),
307-
'groups' => ['basic', 'strict'],
308-
]),
309-
'email' => new Assert\Required([
310-
"constraints" => new Assert\NotBlank(['groups' => 'contact']),
311-
'groups' => ['basic', 'strict'],
312-
]),
303+
$constraint = new Assert\Collection(
304+
fields: [
305+
'name' => new Assert\Required(
306+
constraints: new Assert\NotBlank(groups: ['basic']),
307+
groups: ['basic', 'strict'],
308+
),
309+
'email' => new Assert\Required(
310+
constraints: new Assert\NotBlank(groups: ['contact']),
311+
groups: ['basic', 'strict'],
312+
),
313313
],
314-
'groups' => ['basic', 'strict'],
315-
]);
314+
groups: ['basic', 'strict'],
315+
);
316316

317317
The default ``allowMissingFields`` option requires the fields in all groups.
318318
So when validating in ``contact`` group, ``$name`` can be empty but the key is
@@ -360,7 +360,7 @@ Parameter Description
360360
``fields``
361361
~~~~~~~~~~
362362

363-
**type**: ``array`` [:ref:`default option <validation-default-option>`]
363+
**type**: ``array``
364364

365365
This option is required and is an associative array defining all of the
366366
keys in the collection and, for each key, exactly which validator(s) should

Diff for: reference/constraints/Compound.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ you can create your own named set or requirements to be reused consistently ever
3535
return [
3636
new Assert\NotBlank(),
3737
new Assert\Type('string'),
38-
new Assert\Length(['min' => 12]),
38+
new Assert\Length(min: 12),
3939
new Assert\NotCompromisedPassword(),
40-
new Assert\PasswordStrength(['minScore' => 4]),
40+
new Assert\PasswordStrength(minScore: 4),
4141
];
4242
}
4343
}

Diff for: reference/constraints/Count.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ you might add the following:
8282
8383
public static function loadValidatorMetadata(ClassMetadata $metadata): void
8484
{
85-
$metadata->addPropertyConstraint('emails', new Assert\Count([
86-
'min' => 1,
87-
'max' => 5,
88-
'minMessage' => 'You must specify at least one email',
89-
'maxMessage' => 'You cannot specify more than {{ limit }} emails',
90-
]));
85+
$metadata->addPropertyConstraint('emails', new Assert\Count(
86+
min: 1,
87+
max: 5,
88+
minMessage: 'You must specify at least one email',
89+
maxMessage: 'You cannot specify more than {{ limit }} emails',
90+
));
9191
}
9292
}
9393

0 commit comments

Comments
 (0)