Skip to content

Commit

Permalink
use named-arguments to configure validation constraint options
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jan 24, 2025
1 parent 171af72 commit aacdaf3
Show file tree
Hide file tree
Showing 24 changed files with 130 additions and 130 deletions.
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
8 changes: 4 additions & 4 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 Down
24 changes: 12 additions & 12 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 Down
8 changes: 4 additions & 4 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
20 changes: 10 additions & 10 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 Down
44 changes: 22 additions & 22 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
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
18 changes: 9 additions & 9 deletions reference/constraints/CssColor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ the named CSS colors:
{
$metadata->addPropertyConstraint('defaultColor', new Assert\CssColor());
$metadata->addPropertyConstraint('accentColor', new Assert\CssColor([
'formats' => Assert\CssColor::HEX_LONG,
'message' => 'The accent color must be a 6-character hexadecimal color.',
]));
$metadata->addPropertyConstraint('currentColor', new Assert\CssColor([
'formats' => [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS],
'message' => 'The color "{{ value }}" is not a valid CSS color name.',
]));
$metadata->addPropertyConstraint('accentColor', new Assert\CssColor(
formats: Assert\CssColor::HEX_LONG,
message: 'The accent color must be a 6-character hexadecimal color.',
));
$metadata->addPropertyConstraint('currentColor', new Assert\CssColor(
formats: [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS],
message: 'The color "{{ value }}" is not a valid CSS color name.',
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/DivisibleBy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ The following constraints ensure that:
{
$metadata->addPropertyConstraint('weight', new Assert\DivisibleBy(0.25));
$metadata->addPropertyConstraint('quantity', new Assert\DivisibleBy([
'value' => 5,
]));
$metadata->addPropertyConstraint('quantity', new Assert\DivisibleBy(
value: 5,
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/Email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ Basic Usage
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('email', new Assert\Email([
'message' => 'The email "{{ value }}" is not a valid email.',
]));
$metadata->addPropertyConstraint('email', new Assert\Email(
message: 'The email "{{ value }}" is not a valid email.',
));
}
}
Expand Down
Loading

0 comments on commit aacdaf3

Please sign in to comment.