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 239a970
Show file tree
Hide file tree
Showing 40 changed files with 177 additions and 259 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
2 changes: 1 addition & 1 deletion reference/constraints/All.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ entry in that array:
$metadata->addPropertyConstraint('favoriteColors', new Assert\All([
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['min' => 5]),
new Assert\Length(min: 5),
],
]));
}
Expand Down
12 changes: 6 additions & 6 deletions reference/constraints/AtLeastOneOf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ The following constraints ensure that:
{
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([
'constraints' => [
new Assert\Regex(['pattern' => '/#/']),
new Assert\Length(['min' => 10]),
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' => [
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
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
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
10 changes: 5 additions & 5 deletions reference/constraints/File.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ below a certain file size and a valid PDF, add the following:
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('bioFile', new Assert\File([
'maxSize' => '1024k',
'extensions' => [
$metadata->addPropertyConstraint('bioFile', new Assert\File(
maxSize: '1024k',
extensions: [
'pdf',
],
'extensionsMessage' => 'Please upload a valid PDF',
]));
extensionsMessage: 'Please upload a valid PDF',
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/Hostname.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ will contain a host name.
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('name', new Assert\Hostname([
'message' => 'The server name must be a valid hostname.',
]));
$metadata->addPropertyConstraint('name', new Assert\Hostname(
message: 'The server name must be a valid hostname.',
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/Iban.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ will contain an International Bank Account Number.
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban([
'message' => 'This is not a valid International Bank Account Number (IBAN).',
]));
$metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban(
message: 'This is not a valid International Bank Account Number (IBAN).',
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/IdenticalTo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ The following constraints ensure that:
{
$metadata->addPropertyConstraint('firstName', new Assert\IdenticalTo('Mary'));
$metadata->addPropertyConstraint('age', new Assert\IdenticalTo([
'value' => 20,
]));
$metadata->addPropertyConstraint('age', new Assert\IdenticalTo(
value: 20,
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/IsFalse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ method returns **false**:
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse([
'message' => "You've entered an invalid state.",
]));
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse(
message: "You've entered an invalid state.",
));
}
public function isStateInvalid(): bool
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/IsTrue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ Then you can validate this method with ``IsTrue`` as follows:
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addGetterConstraint('tokenValid', new IsTrue([
'message' => 'The token is invalid.',
]));
$metadata->addGetterConstraint('tokenValid', new IsTrue(
message: 'The token is invalid.',
));
}
public function isTokenValid(): bool
Expand Down
8 changes: 4 additions & 4 deletions reference/constraints/Isbn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ on an object that will contain an ISBN.
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('isbn', new Assert\Isbn([
'type' => Assert\Isbn::ISBN_10,
'message' => 'This value is not valid.',
]));
$metadata->addPropertyConstraint('isbn', new Assert\Isbn(
type: Assert\Isbn::ISBN_10,
message: 'This value is not valid.',
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/Json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ The ``Json`` constraint can be applied to a property or a "getter" method:
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('chapters', new Assert\Json([
'message' => 'You\'ve entered an invalid Json.',
]));
$metadata->addPropertyConstraint('chapters', new Assert\Json(
message: 'You\'ve entered an invalid Json.',
));
}
}
Expand Down
Loading

0 comments on commit 239a970

Please sign in to comment.