Skip to content

Commit 5ba520a

Browse files
committed
bug #17905 Fix the example for the When constraint (stof)
This PR was merged into the 6.2 branch. Discussion ---------- Fix the example for the When constraint When evaluating the expression, the ExpressionLanguage does not have access to private properties of the object, as it runs from its outside. And contrary to Twig, ExpressionLanguage does not have the magic `.` operator that tries to find a getter when it cannot use the property. See symfony/symfony#49367 for a support request caused by this bad example. Commits ------- 7ad1447 Fix the example for the When constraint
2 parents e1f30ff + 7ad1447 commit 5ba520a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

reference/constraints/When.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ One way to accomplish this is with the When constraint:
6969
{
7070
#[Assert\GreaterThan(0)]
7171
#[Assert\When(
72-
expression: 'this.type == "percent"',
72+
expression: 'this.getType() == "percent"',
7373
constraints: [
7474
new Assert\LessThanOrEqual(100, message: 'The value should be between 1 and 100!')
7575
],
@@ -87,7 +87,7 @@ One way to accomplish this is with the When constraint:
8787
value:
8888
- GreaterThan: 0
8989
- When:
90-
expression: "this.type == 'percent'"
90+
expression: "this.getType() == 'percent'"
9191
constraints:
9292
- LessThanOrEqual:
9393
value: 100
@@ -105,7 +105,7 @@ One way to accomplish this is with the When constraint:
105105
<constraint name="GreaterThan">0</constraint>
106106
<constraint name="When">
107107
<option name="expression">
108-
this.type == 'percent'
108+
this.getType() == 'percent'
109109
</option>
110110
<option name="constraints">
111111
<constraint name="LessThanOrEqual">
@@ -132,7 +132,7 @@ One way to accomplish this is with the When constraint:
132132
{
133133
$metadata->addPropertyConstraint('value', new Assert\GreaterThan(0));
134134
$metadata->addPropertyConstraint('value', new Assert\When([
135-
'expression' => 'this.type == "percent"',
135+
'expression' => 'this.getType() == "percent"',
136136
'constraints' => [
137137
new Assert\LessThanOrEqual([
138138
'value' => 100,

0 commit comments

Comments
 (0)