Skip to content

Commit b753629

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

File tree

6 files changed

+23
-107
lines changed

6 files changed

+23
-107
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

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
// ...

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

0 commit comments

Comments
 (0)