Skip to content

Commit 35d8c97

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

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

reference/constraints/All.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ entry in that array:
7979
{
8080
public static function loadValidatorMetadata(ClassMetadata $metadata): void
8181
{
82-
$metadata->addPropertyConstraint('favoriteColors', new Assert\All([
83-
'constraints' => [
82+
$metadata->addPropertyConstraint('favoriteColors', new Assert\All(
83+
constraints: [
8484
new Assert\NotBlank(),
85-
new Assert\Length(['min' => 5]),
85+
new Assert\Length(min: 5),
8686
],
87-
]));
87+
));
8888
}
8989
}
9090

reference/constraints/AtLeastOneOf.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,23 @@ The following constraints ensure that:
115115
{
116116
public static function loadValidatorMetadata(ClassMetadata $metadata): void
117117
{
118-
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([
119-
'constraints' => [
120-
new Assert\Regex(['pattern' => '/#/']),
121-
new Assert\Length(['min' => 10]),
118+
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf(
119+
constraints: [
120+
new Assert\Regex(pattern: '/#/'),
121+
new Assert\Length(min: 10),
122122
],
123-
]));
123+
));
124124
125-
$metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf([
126-
'constraints' => [
127-
new Assert\Count(['min' => 3]),
128-
new Assert\All([
129-
'constraints' => [
125+
$metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf(
126+
constraints: [
127+
new Assert\Count(min: 3),
128+
new Assert\All(
129+
constraints: [
130130
new Assert\GreaterThanOrEqual(5),
131131
],
132-
]),
132+
),
133133
],
134-
]));
134+
));
135135
}
136136
}
137137

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ One way to accomplish this is with the Expression constraint:
111111
{
112112
public static function loadValidatorMetadata(ClassMetadata $metadata): void
113113
{
114-
$metadata->addConstraint(new Assert\Expression([
115-
'expression' => 'this.getCategory() in ["php", "symfony"] or !this.isTechnicalPost()',
116-
'message' => 'If this is a tech post, the category should be either php or symfony!',
117-
]));
114+
$metadata->addConstraint(new Assert\Expression(
115+
expression: 'this.getCategory() in ["php", "symfony"] or !this.isTechnicalPost()',
116+
message: 'If this is a tech post, the category should be either php or symfony!',
117+
));
118118
}
119119
120120
// ...
@@ -200,10 +200,10 @@ assert that the expression must return ``true`` for validation to fail.
200200
{
201201
public static function loadValidatorMetadata(ClassMetadata $metadata): void
202202
{
203-
$metadata->addPropertyConstraint('isTechnicalPost', new Assert\Expression([
204-
'expression' => 'this.getCategory() in ["php", "symfony"] or value == false',
205-
'message' => 'If this is a tech post, the category should be either php or symfony!',
206-
]));
203+
$metadata->addPropertyConstraint('isTechnicalPost', new Assert\Expression(
204+
expression: 'this.getCategory() in ["php", "symfony"] or value == false',
205+
message: 'If this is a tech post, the category should be either php or symfony!',
206+
));
207207
}
208208
209209
// ...
@@ -343,10 +343,10 @@ type (numeric, boolean, strings, null, etc.)
343343
{
344344
public static function loadValidatorMetadata(ClassMetadata $metadata): void
345345
{
346-
$metadata->addPropertyConstraint('metric', new Assert\Expression([
347-
'expression' => 'value + error_margin < threshold',
348-
'values' => ['error_margin' => 0.25, 'threshold' => 1.5],
349-
]));
346+
$metadata->addPropertyConstraint('metric', new Assert\Expression(
347+
expression: 'value + error_margin < threshold',
348+
values: ['error_margin' => 0.25, 'threshold' => 1.5],
349+
));
350350
}
351351
352352
// ...

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

0 commit comments

Comments
 (0)