Skip to content

[Validator] use named-arguments to configure validation constraint options #20596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions components/console/helpers/questionhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,10 @@ invalid answer and will only be able to proceed if their input is valid.
use Symfony\Component\Validator\Validation;

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

Validating a Hidden Response
Expand Down
2 changes: 1 addition & 1 deletion components/options_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ returns ``true`` for acceptable values and ``false`` for invalid values::

// ...
$resolver->setAllowedValues('transport', Validation::createIsValidCallable(
new Length(['min' => 10 ])
new Length(min: 10)
));

In sub-classes, you can use :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedValues`
Expand Down
2 changes: 1 addition & 1 deletion components/validator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ characters long::

$validator = Validation::createValidator();
$violations = $validator->validate('Bernhard', [
new Length(['min' => 10]),
new Length(min: 10),
new NotBlank(),
]);

Expand Down
8 changes: 4 additions & 4 deletions components/validator/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ the ``Author`` class has at least 3 characters::
$metadata->addPropertyConstraint('firstName', new Assert\NotBlank());
$metadata->addPropertyConstraint(
'firstName',
new Assert\Length(["min" => 3])
new Assert\Length(min: 3)
);
}
}
Expand Down Expand Up @@ -55,9 +55,9 @@ Then, add the Validator component configuration to the class::
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue([
'message' => 'The password cannot match your first name',
]));
$metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue(
message: 'The password cannot match your first name',
));
}
}

Expand Down
8 changes: 4 additions & 4 deletions components/validator/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ In this example, the validation metadata is retrieved executing the
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
$metadata->addPropertyConstraint('name', new Assert\Length([
'min' => 5,
'max' => 20,
]));
$metadata->addPropertyConstraint('name', new Assert\Length(
min: 5,
max: 20,
));
}
}

Expand Down
10 changes: 5 additions & 5 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ so Symfony doesn't try to get/set its value from the related entity::
// unmapped fields can't define their validation using attributes
// in the associated entity, so you can use the PHP constraint classes
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypes' => [
new File(
maxSize: '1024k',
mimeTypes: [
'application/pdf',
'application/x-pdf',
],
'mimeTypesMessage' => 'Please upload a valid PDF document',
])
mimeTypesMessage: 'Please upload a valid PDF document',
)
],
])
// ...
Expand Down
8 changes: 4 additions & 4 deletions form/without_class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ but here's a short example::
{
$builder
->add('firstName', TextType::class, [
'constraints' => new Length(['min' => 3]),
'constraints' => new Length(min: 3),
])
->add('lastName', TextType::class, [
'constraints' => [
new NotBlank(),
new Length(['min' => 3]),
new Length(min: 3),
],
])
;
Expand Down Expand Up @@ -153,10 +153,10 @@ This can be done by setting the ``constraints`` option in the
$resolver->setDefaults([
'data_class' => null,
'constraints' => new Collection([
'firstName' => new Length(['min' => 3]),
'firstName' => new Length(min: 3),
'lastName' => [
new NotBlank(),
new Length(['min' => 3]),
new Length(min: 3),
],
]),
]);
Expand Down
10 changes: 5 additions & 5 deletions reference/constraints/All.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ entry in that array:
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('favoriteColors', new Assert\All([
'constraints' => [
$metadata->addPropertyConstraint('favoriteColors', new Assert\All(
constraints: [
new Assert\NotBlank(),
new Assert\Length(['min' => 5]),
new Assert\Length(min: 5),
],
]));
));
}
}

Expand All @@ -97,7 +97,7 @@ Options
``constraints``
~~~~~~~~~~~~~~~

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

This required option is the array of validation constraints that you want
to apply to each element of the underlying array.
Expand Down
26 changes: 13 additions & 13 deletions reference/constraints/AtLeastOneOf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,23 @@ The following constraints ensure that:
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([
'constraints' => [
new Assert\Regex(['pattern' => '/#/']),
new Assert\Length(['min' => 10]),
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf(
constraints: [
new Assert\Regex(pattern: '/#/'),
new Assert\Length(min: 10),
],
]));
));

$metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf([
'constraints' => [
new Assert\Count(['min' => 3]),
new Assert\All([
'constraints' => [
$metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf(
constraints: [
new Assert\Count(min: 3),
new Assert\All(
constraints: [
new Assert\GreaterThanOrEqual(5),
],
]),
),
],
]));
));
}
}

Expand All @@ -141,7 +141,7 @@ Options
constraints
~~~~~~~~~~~

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

This required option is the array of validation constraints from which at least one of
has to be satisfied in order for the validation to succeed.
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ Options
``callback``
~~~~~~~~~~~~

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

The callback option accepts three different formats for specifying the
callback method:
Expand Down
10 changes: 5 additions & 5 deletions reference/constraints/CardScheme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ on an object that will contain a credit card number.
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme([
'schemes' => [
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme(
schemes: [
Assert\CardScheme::VISA,
],
'message' => 'Your credit card number is invalid.',
]));
message: 'Your credit card number is invalid.',
));
}
}

Expand Down Expand Up @@ -114,7 +114,7 @@ Parameter Description
``schemes``
~~~~~~~~~~~

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

This option is required and represents the name of the number scheme used
to validate the credit card number, it can either be a string or an array.
Expand Down
22 changes: 11 additions & 11 deletions reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ If your valid choice list is simple, you can pass them in directly via the
new Assert\Choice(['New York', 'Berlin', 'Tokyo'])
);

$metadata->addPropertyConstraint('genre', new Assert\Choice([
'choices' => ['fiction', 'non-fiction'],
'message' => 'Choose a valid genre.',
]));
$metadata->addPropertyConstraint('genre', new Assert\Choice(
choices: ['fiction', 'non-fiction'],
message: 'Choose a valid genre.',
));
}
}

Expand Down Expand Up @@ -182,9 +182,9 @@ constraint.

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('genre', new Assert\Choice([
'callback' => 'getGenres',
]));
$metadata->addPropertyConstraint('genre', new Assert\Choice(
callback: 'getGenres',
));
}
}

Expand Down Expand Up @@ -250,9 +250,9 @@ you can pass the class name and the method as an array.

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('genre', new Assert\Choice([
'callback' => [Genre::class, 'getGenres'],
]));
$metadata->addPropertyConstraint('genre', new Assert\Choice(
callback: [Genre::class, 'getGenres'],
));
}
}

Expand All @@ -271,7 +271,7 @@ to return the choices array. See
``choices``
~~~~~~~~~~~

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

A required option (unless `callback`_ is specified) - this is the array
of options that should be considered in the valid set. The input value
Expand Down
46 changes: 23 additions & 23 deletions reference/constraints/Collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ following:

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('profileData', new Assert\Collection([
'fields' => [
$metadata->addPropertyConstraint('profileData', new Assert\Collection(
fields: [
'personal_email' => new Assert\Email(),
'short_bio' => [
new Assert\NotBlank(),
Expand All @@ -150,8 +150,8 @@ following:
]),
],
],
'allowMissingFields' => true,
]));
allowMissingFields: true,
));
}
}

Expand Down Expand Up @@ -267,15 +267,15 @@ you can do the following:

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('profileData', new Assert\Collection([
'fields' => [
$metadata->addPropertyConstraint('profileData', new Assert\Collection(
fields: [
'personal_email' => new Assert\Required([
new Assert\NotBlank(),
new Assert\Email(),
]),
'alternate_email' => new Assert\Optional(new Assert\Email()),
],
]));
));
}
}

Expand All @@ -291,28 +291,28 @@ groups. Take the following example::

use Symfony\Component\Validator\Constraints as Assert;

$constraint = new Assert\Collection([
'fields' => [
$constraint = new Assert\Collection(
fields: [
'name' => new Assert\NotBlank(['groups' => 'basic']),
'email' => new Assert\NotBlank(['groups' => 'contact']),
],
]);
);

This will result in the following configuration::

$constraint = new Assert\Collection([
'fields' => [
'name' => new Assert\Required([
'constraints' => new Assert\NotBlank(['groups' => 'basic']),
'groups' => ['basic', 'strict'],
]),
'email' => new Assert\Required([
"constraints" => new Assert\NotBlank(['groups' => 'contact']),
'groups' => ['basic', 'strict'],
]),
$constraint = new Assert\Collection(
fields: [
'name' => new Assert\Required(
constraints: new Assert\NotBlank(groups: ['basic']),
groups: ['basic', 'strict'],
),
'email' => new Assert\Required(
constraints: new Assert\NotBlank(groups: ['contact']),
groups: ['basic', 'strict'],
),
],
'groups' => ['basic', 'strict'],
]);
groups: ['basic', 'strict'],
);

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

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

This option is required and is an associative array defining all of the
keys in the collection and, for each key, exactly which validator(s) should
Expand Down
4 changes: 2 additions & 2 deletions reference/constraints/Compound.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ you can create your own named set or requirements to be reused consistently ever
return [
new Assert\NotBlank(),
new Assert\Type('string'),
new Assert\Length(['min' => 12]),
new Assert\Length(min: 12),
new Assert\NotCompromisedPassword(),
new Assert\PasswordStrength(['minScore' => 4]),
new Assert\PasswordStrength(minScore: 4),
];
}
}
Expand Down
12 changes: 6 additions & 6 deletions reference/constraints/Count.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ you might add the following:

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('emails', new Assert\Count([
'min' => 1,
'max' => 5,
'minMessage' => 'You must specify at least one email',
'maxMessage' => 'You cannot specify more than {{ limit }} emails',
]));
$metadata->addPropertyConstraint('emails', new Assert\Count(
min: 1,
max: 5,
minMessage: 'You must specify at least one email',
maxMessage: 'You cannot specify more than {{ limit }} emails',
));
}
}

Expand Down
Loading
Loading