Skip to content

Commit 87aa029

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

File tree

13 files changed

+75
-157
lines changed

13 files changed

+75
-157
lines changed

components/console/helpers/questionhelper.rst

Lines changed: 4 additions & 4 deletions
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

components/options_resolver.rst

Lines changed: 1 addition & 1 deletion
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`

components/validator.rst

Lines changed: 1 addition & 1 deletion
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

components/validator/metadata.rst

Lines changed: 4 additions & 4 deletions
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

components/validator/resources.rst

Lines changed: 4 additions & 4 deletions
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

controller/upload_file.rst

Lines changed: 5 additions & 5 deletions
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
// ...

form/without_class.rst

Lines changed: 4 additions & 4 deletions
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
]);

validation.rst

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -327,99 +327,15 @@ literature genre mostly associated with the author, which can be set to either
327327
{
328328
// ...
329329
330-
$metadata->addPropertyConstraint('genre', new Assert\Choice([
331-
'choices' => ['fiction', 'non-fiction'],
332-
'message' => 'Choose a valid genre.',
333-
]));
330+
$metadata->addPropertyConstraint('genre', new Assert\Choice(
331+
choices: ['fiction', 'non-fiction'],
332+
message: 'Choose a valid genre.',
333+
));
334334
}
335335
}
336336
337337
.. _validation-default-option:
338338

339-
The options of a constraint can always be passed in as an array. Some constraints,
340-
however, also allow you to pass the value of one, "*default*", option in place
341-
of the array. In the case of the ``Choice`` constraint, the ``choices``
342-
options can be specified in this way.
343-
344-
.. configuration-block::
345-
346-
.. code-block:: php-attributes
347-
348-
// src/Entity/Author.php
349-
namespace App\Entity;
350-
351-
// ...
352-
use Symfony\Component\Validator\Constraints as Assert;
353-
354-
class Author
355-
{
356-
#[Assert\Choice(['fiction', 'non-fiction'])]
357-
private string $genre;
358-
359-
// ...
360-
}
361-
362-
.. code-block:: yaml
363-
364-
# config/validator/validation.yaml
365-
App\Entity\Author:
366-
properties:
367-
genre:
368-
- Choice: [fiction, non-fiction]
369-
# ...
370-
371-
.. code-block:: xml
372-
373-
<!-- config/validator/validation.xml -->
374-
<?xml version="1.0" encoding="UTF-8" ?>
375-
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
376-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
377-
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
378-
https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
379-
380-
<class name="App\Entity\Author">
381-
<property name="genre">
382-
<constraint name="Choice">
383-
<value>fiction</value>
384-
<value>non-fiction</value>
385-
</constraint>
386-
</property>
387-
388-
<!-- ... -->
389-
</class>
390-
</constraint-mapping>
391-
392-
.. code-block:: php
393-
394-
// src/Entity/Author.php
395-
namespace App\Entity;
396-
397-
// ...
398-
use Symfony\Component\Validator\Constraints as Assert;
399-
use Symfony\Component\Validator\Mapping\ClassMetadata;
400-
401-
class Author
402-
{
403-
private string $genre;
404-
405-
public static function loadValidatorMetadata(ClassMetadata $metadata): void
406-
{
407-
// ...
408-
409-
$metadata->addPropertyConstraint(
410-
'genre',
411-
new Assert\Choice(['fiction', 'non-fiction'])
412-
);
413-
}
414-
}
415-
416-
This is purely meant to make the configuration of the most common option of
417-
a constraint shorter and quicker.
418-
419-
If you're ever unsure of how to specify an option, either check the namespace
420-
``Symfony\Component\Validator\Constraints`` for the constraint or play it safe
421-
by always passing in an array of options (the first method shown above).
422-
423339
Constraints in Form Classes
424340
---------------------------
425341

@@ -520,7 +436,7 @@ class to have at least 3 characters.
520436
$metadata->addPropertyConstraint('firstName', new Assert\NotBlank());
521437
$metadata->addPropertyConstraint(
522438
'firstName',
523-
new Assert\Length(['min' => 3])
439+
new Assert\Length(min: 3)
524440
);
525441
}
526442
}
@@ -603,9 +519,9 @@ this method must return ``true``:
603519
{
604520
public static function loadValidatorMetadata(ClassMetadata $metadata): void
605521
{
606-
$metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue([
607-
'message' => 'The password cannot match your first name',
608-
]));
522+
$metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue(
523+
message: 'The password cannot match your first name',
524+
));
609525
}
610526
}
611527

validation/custom_constraint.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ You can use custom validators like the ones provided by Symfony itself:
248248
public static function loadValidatorMetadata(ClassMetadata $metadata): void
249249
{
250250
$metadata->addPropertyConstraint('name', new NotBlank());
251-
$metadata->addPropertyConstraint('name', new ContainsAlphanumeric(['mode' => 'loose']));
251+
$metadata->addPropertyConstraint('name', new ContainsAlphanumeric(mode: 'loose'));
252252
}
253253
}
254254
@@ -273,6 +273,7 @@ define those options as public properties on the constraint class::
273273
// src/Validator/Foo.php
274274
namespace App\Validator;
275275

276+
use Symfony\Component\Validator\Attribute\HasNamedArguments;
276277
use Symfony\Component\Validator\Constraint;
277278

278279
#[\Attribute]
@@ -282,6 +283,7 @@ define those options as public properties on the constraint class::
282283
public $message = 'This value is invalid';
283284
public $optionalBarOption = false;
284285

286+
#[HasNamedArguments]
285287
public function __construct(
286288
$mandatoryFooOption,
287289
?string $message = null,
@@ -402,10 +404,10 @@ the custom options like you pass any other option in built-in constraints:
402404
public static function loadValidatorMetadata(ClassMetadata $metadata)
403405
{
404406
$metadata->addPropertyConstraint('name', new NotBlank());
405-
$metadata->addPropertyConstraint('name', new Foo([
406-
'mandatoryFooOption' => 'bar',
407-
'optionalBarOption' => true,
408-
]));
407+
$metadata->addPropertyConstraint('name', new Foo(
408+
mandatoryFooOption: 'bar',
409+
optionalBarOption: true,
410+
));
409411
}
410412
}
411413

validation/groups.rst

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,21 @@ user registers and when a user updates their contact information later:
101101
{
102102
public static function loadValidatorMetadata(ClassMetadata $metadata): void
103103
{
104-
$metadata->addPropertyConstraint('email', new Assert\Email([
105-
'groups' => ['registration'],
106-
]));
107-
108-
$metadata->addPropertyConstraint('password', new Assert\NotBlank([
109-
'groups' => ['registration'],
110-
]));
111-
$metadata->addPropertyConstraint('password', new Assert\Length([
112-
'min' => 7,
113-
'groups' => ['registration'],
114-
]));
115-
116-
$metadata->addPropertyConstraint('city', new Assert\Length([
117-
'min' => 2,
118-
]));
104+
$metadata->addPropertyConstraint('email', new Assert\Email(
105+
groups: ['registration'],
106+
));
107+
108+
$metadata->addPropertyConstraint('password', new Assert\NotBlank(
109+
groups: ['registration'],
110+
));
111+
$metadata->addPropertyConstraint('password', new Assert\Length(
112+
min: 7,
113+
groups: ['registration'],
114+
));
115+
116+
$metadata->addPropertyConstraint('city', new Assert\Length(
117+
min: 2,
118+
));
119119
}
120120
}
121121

0 commit comments

Comments
 (0)