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 a5e6426
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 99 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 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
6 changes: 3 additions & 3 deletions reference/constraints/EqualTo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ and that the ``age`` is ``20``, you could do the following:
{
$metadata->addPropertyConstraint('firstName', new Assert\EqualTo('Mary'));
$metadata->addPropertyConstraint('age', new Assert\EqualTo([
'value' => 20,
]));
$metadata->addPropertyConstraint('age', new Assert\EqualTo(
value: 20,
));
}
}
Expand Down
24 changes: 12 additions & 12 deletions reference/constraints/Expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ One way to accomplish this is with the Expression constraint:
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addConstraint(new Assert\Expression([
'expression' => 'this.getCategory() in ["php", "symfony"] or !this.isTechnicalPost()',
'message' => 'If this is a tech post, the category should be either php or symfony!',
]));
$metadata->addConstraint(new Assert\Expression(
expression: 'this.getCategory() in ["php", "symfony"] or !this.isTechnicalPost()',
message: 'If this is a tech post, the category should be either php or symfony!',
));
}
// ...
Expand Down Expand Up @@ -200,10 +200,10 @@ assert that the expression must return ``true`` for validation to fail.
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('isTechnicalPost', new Assert\Expression([
'expression' => 'this.getCategory() in ["php", "symfony"] or value == false',
'message' => 'If this is a tech post, the category should be either php or symfony!',
]));
$metadata->addPropertyConstraint('isTechnicalPost', new Assert\Expression(
expression: 'this.getCategory() in ["php", "symfony"] or value == false',
message: 'If this is a tech post, the category should be either php or symfony!',
));
}
// ...
Expand Down Expand Up @@ -343,10 +343,10 @@ type (numeric, boolean, strings, null, etc.)
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('metric', new Assert\Expression([
'expression' => 'value + error_margin < threshold',
'values' => ['error_margin' => 0.25, 'threshold' => 1.5],
]));
$metadata->addPropertyConstraint('metric', new Assert\Expression(
expression: 'value + error_margin < threshold',
values: ['error_margin' => 0.25, 'threshold' => 1.5],
));
}
// ...
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/ExpressionSyntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ The following constraints ensure that:
{
$metadata->addPropertyConstraint('promotion', new Assert\ExpressionSyntax());
$metadata->addPropertyConstraint('shippingOptions', new Assert\ExpressionSyntax([
'allowedVariables' => ['user', 'shipping_centers'],
]));
$metadata->addPropertyConstraint('shippingOptions', new Assert\ExpressionSyntax(
allowedVariables: ['user', 'shipping_centers'],
));
}
}
Expand Down

0 comments on commit a5e6426

Please sign in to comment.