Skip to content

Commit 239a970

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

40 files changed

+177
-259
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

components/validator/resources.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ In this example, the validation metadata is retrieved executing the
4242
public static function loadValidatorMetadata(ClassMetadata $metadata): void
4343
{
4444
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
45-
$metadata->addPropertyConstraint('name', new Assert\Length([
46-
'min' => 5,
47-
'max' => 20,
48-
]));
45+
$metadata->addPropertyConstraint('name', new Assert\Length(
46+
min: 5,
47+
max: 20,
48+
));
4949
}
5050
}
5151

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

form/without_class.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ but here's a short example::
9696
{
9797
$builder
9898
->add('firstName', TextType::class, [
99-
'constraints' => new Length(['min' => 3]),
99+
'constraints' => new Length(min: 3),
100100
])
101101
->add('lastName', TextType::class, [
102102
'constraints' => [
103103
new NotBlank(),
104-
new Length(['min' => 3]),
104+
new Length(min: 3),
105105
],
106106
])
107107
;
@@ -153,10 +153,10 @@ This can be done by setting the ``constraints`` option in the
153153
$resolver->setDefaults([
154154
'data_class' => null,
155155
'constraints' => new Collection([
156-
'firstName' => new Length(['min' => 3]),
156+
'firstName' => new Length(min: 3),
157157
'lastName' => [
158158
new NotBlank(),
159-
new Length(['min' => 3]),
159+
new Length(min: 3),
160160
],
161161
]),
162162
]);

reference/constraints/All.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ entry in that array:
8282
$metadata->addPropertyConstraint('favoriteColors', new Assert\All([
8383
'constraints' => [
8484
new Assert\NotBlank(),
85-
new Assert\Length(['min' => 5]),
85+
new Assert\Length(min: 5),
8686
],
8787
]));
8888
}

reference/constraints/AtLeastOneOf.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,19 @@ The following constraints ensure that:
117117
{
118118
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([
119119
'constraints' => [
120-
new Assert\Regex(['pattern' => '/#/']),
121-
new Assert\Length(['min' => 10]),
120+
new Assert\Regex(pattern: '/#/'),
121+
new Assert\Length(min: 10),
122122
],
123123
]));
124124
125125
$metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf([
126126
'constraints' => [
127-
new Assert\Count(['min' => 3]),
128-
new Assert\All([
129-
'constraints' => [
127+
new Assert\Count(min: 3),
128+
new Assert\All(
129+
'constraints: [
130130
new Assert\GreaterThanOrEqual(5),
131131
],
132-
]),
132+
),
133133
],
134134
]));
135135
}

reference/constraints/CardScheme.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ on an object that will contain a credit card number.
7777
{
7878
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7979
{
80-
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme([
81-
'schemes' => [
80+
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme(
81+
schemes: [
8282
Assert\CardScheme::VISA,
8383
],
84-
'message' => 'Your credit card number is invalid.',
85-
]));
84+
message: 'Your credit card number is invalid.',
85+
));
8686
}
8787
}
8888

reference/constraints/Compound.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ you can create your own named set or requirements to be reused consistently ever
3535
return [
3636
new Assert\NotBlank(),
3737
new Assert\Type('string'),
38-
new Assert\Length(['min' => 12]),
38+
new Assert\Length(min: 12),
3939
new Assert\NotCompromisedPassword(),
40-
new Assert\PasswordStrength(['minScore' => 4]),
40+
new Assert\PasswordStrength(minScore: 4),
4141
];
4242
}
4343
}

reference/constraints/Count.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ you might add the following:
8282
8383
public static function loadValidatorMetadata(ClassMetadata $metadata): void
8484
{
85-
$metadata->addPropertyConstraint('emails', new Assert\Count([
86-
'min' => 1,
87-
'max' => 5,
88-
'minMessage' => 'You must specify at least one email',
89-
'maxMessage' => 'You cannot specify more than {{ limit }} emails',
90-
]));
85+
$metadata->addPropertyConstraint('emails', new Assert\Count(
86+
min: 1,
87+
max: 5,
88+
minMessage: 'You must specify at least one email',
89+
maxMessage: 'You cannot specify more than {{ limit }} emails',
90+
));
9191
}
9292
}
9393

reference/constraints/CssColor.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ the named CSS colors:
110110
{
111111
$metadata->addPropertyConstraint('defaultColor', new Assert\CssColor());
112112
113-
$metadata->addPropertyConstraint('accentColor', new Assert\CssColor([
114-
'formats' => Assert\CssColor::HEX_LONG,
115-
'message' => 'The accent color must be a 6-character hexadecimal color.',
116-
]));
117-
118-
$metadata->addPropertyConstraint('currentColor', new Assert\CssColor([
119-
'formats' => [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS],
120-
'message' => 'The color "{{ value }}" is not a valid CSS color name.',
121-
]));
113+
$metadata->addPropertyConstraint('accentColor', new Assert\CssColor(
114+
formats: Assert\CssColor::HEX_LONG,
115+
message: 'The accent color must be a 6-character hexadecimal color.',
116+
));
117+
118+
$metadata->addPropertyConstraint('currentColor', new Assert\CssColor(
119+
formats: [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS],
120+
message: 'The color "{{ value }}" is not a valid CSS color name.',
121+
));
122122
}
123123
}
124124

reference/constraints/DivisibleBy.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ The following constraints ensure that:
9292
{
9393
$metadata->addPropertyConstraint('weight', new Assert\DivisibleBy(0.25));
9494
95-
$metadata->addPropertyConstraint('quantity', new Assert\DivisibleBy([
96-
'value' => 5,
97-
]));
95+
$metadata->addPropertyConstraint('quantity', new Assert\DivisibleBy(
96+
value: 5,
97+
));
9898
}
9999
}
100100

reference/constraints/Email.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ Basic Usage
7070
7171
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7272
{
73-
$metadata->addPropertyConstraint('email', new Assert\Email([
74-
'message' => 'The email "{{ value }}" is not a valid email.',
75-
]));
73+
$metadata->addPropertyConstraint('email', new Assert\Email(
74+
message: 'The email "{{ value }}" is not a valid email.',
75+
));
7676
}
7777
}
7878

reference/constraints/EqualTo.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ and that the ``age`` is ``20``, you could do the following:
9191
{
9292
$metadata->addPropertyConstraint('firstName', new Assert\EqualTo('Mary'));
9393
94-
$metadata->addPropertyConstraint('age', new Assert\EqualTo([
95-
'value' => 20,
96-
]));
94+
$metadata->addPropertyConstraint('age', new Assert\EqualTo(
95+
value: 20,
96+
));
9797
}
9898
}
9999

reference/constraints/ExpressionSyntax.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ The following constraints ensure that:
9090
{
9191
$metadata->addPropertyConstraint('promotion', new Assert\ExpressionSyntax());
9292
93-
$metadata->addPropertyConstraint('shippingOptions', new Assert\ExpressionSyntax([
94-
'allowedVariables' => ['user', 'shipping_centers'],
95-
]));
93+
$metadata->addPropertyConstraint('shippingOptions', new Assert\ExpressionSyntax(
94+
allowedVariables: ['user', 'shipping_centers'],
95+
));
9696
}
9797
}
9898

reference/constraints/File.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ below a certain file size and a valid PDF, add the following:
119119
120120
public static function loadValidatorMetadata(ClassMetadata $metadata): void
121121
{
122-
$metadata->addPropertyConstraint('bioFile', new Assert\File([
123-
'maxSize' => '1024k',
124-
'extensions' => [
122+
$metadata->addPropertyConstraint('bioFile', new Assert\File(
123+
maxSize: '1024k',
124+
extensions: [
125125
'pdf',
126126
],
127-
'extensionsMessage' => 'Please upload a valid PDF',
128-
]));
127+
extensionsMessage: 'Please upload a valid PDF',
128+
));
129129
}
130130
}
131131

reference/constraints/Hostname.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ will contain a host name.
7272
7373
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7474
{
75-
$metadata->addPropertyConstraint('name', new Assert\Hostname([
76-
'message' => 'The server name must be a valid hostname.',
77-
]));
75+
$metadata->addPropertyConstraint('name', new Assert\Hostname(
76+
message: 'The server name must be a valid hostname.',
77+
));
7878
}
7979
}
8080

reference/constraints/Iban.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ will contain an International Bank Account Number.
7777
7878
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7979
{
80-
$metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban([
81-
'message' => 'This is not a valid International Bank Account Number (IBAN).',
82-
]));
80+
$metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban(
81+
message: 'This is not a valid International Bank Account Number (IBAN).',
82+
));
8383
}
8484
}
8585

reference/constraints/IdenticalTo.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ The following constraints ensure that:
9494
{
9595
$metadata->addPropertyConstraint('firstName', new Assert\IdenticalTo('Mary'));
9696
97-
$metadata->addPropertyConstraint('age', new Assert\IdenticalTo([
98-
'value' => 20,
99-
]));
97+
$metadata->addPropertyConstraint('age', new Assert\IdenticalTo(
98+
value: 20,
99+
));
100100
}
101101
}
102102

reference/constraints/IsFalse.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ method returns **false**:
9393
9494
public static function loadValidatorMetadata(ClassMetadata $metadata): void
9595
{
96-
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse([
97-
'message' => "You've entered an invalid state.",
98-
]));
96+
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse(
97+
message: "You've entered an invalid state.",
98+
));
9999
}
100100
101101
public function isStateInvalid(): bool

reference/constraints/IsTrue.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ Then you can validate this method with ``IsTrue`` as follows:
9797
9898
public static function loadValidatorMetadata(ClassMetadata $metadata): void
9999
{
100-
$metadata->addGetterConstraint('tokenValid', new IsTrue([
101-
'message' => 'The token is invalid.',
102-
]));
100+
$metadata->addGetterConstraint('tokenValid', new IsTrue(
101+
message: 'The token is invalid.',
102+
));
103103
}
104104
105105
public function isTokenValid(): bool

reference/constraints/Isbn.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ on an object that will contain an ISBN.
7676
7777
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7878
{
79-
$metadata->addPropertyConstraint('isbn', new Assert\Isbn([
80-
'type' => Assert\Isbn::ISBN_10,
81-
'message' => 'This value is not valid.',
82-
]));
79+
$metadata->addPropertyConstraint('isbn', new Assert\Isbn(
80+
type: Assert\Isbn::ISBN_10,
81+
message: 'This value is not valid.',
82+
));
8383
}
8484
}
8585

reference/constraints/Json.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ The ``Json`` constraint can be applied to a property or a "getter" method:
6969
{
7070
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7171
{
72-
$metadata->addPropertyConstraint('chapters', new Assert\Json([
73-
'message' => 'You\'ve entered an invalid Json.',
74-
]));
72+
$metadata->addPropertyConstraint('chapters', new Assert\Json(
73+
message: 'You\'ve entered an invalid Json.',
74+
));
7575
}
7676
}
7777

0 commit comments

Comments
 (0)