8000 bug #24750 [Validator] ExpressionValidator should use OBJECT_TO_STRIN… · dunglas/symfony@5a6e2d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a6e2d7

Browse files
committed
bug symfony#24750 [Validator] ExpressionValidator should use OBJECT_TO_STRING (Simperfit)
This PR was merged into the 2.7 branch. Discussion ---------- [Validator] ExpressionValidator should use OBJECT_TO_STRING …value in message | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#24595 | License | MIT | Doc PR | Will do. Commits ------- 7dac528 [Validator] ExpressionValidator should use OBJECT_TO_STRING to allow value in message
2 parents e2c608a + 7dac528 commit 5a6e2d7

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/Symfony/Component/Validator/Constraints/ExpressionValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public function validate($value, Constraint $constraint)
7878
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
7979
if ($this->context instanceof ExecutionContextInterface) {
8080
$this->context->buildViolation($constraint->message)
81-
->setParameter('{{ value }}', $this->formatValue($value))
81+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
8282
->addViolation();
8383
} else {
8484
$this->buildViolation($constraint->message)
85-
->setParameter('{{ value }}', $this->formatValue($value))
85+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
8686
->addViolation();
8787
}
8888
}

src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraints\Expression;
1616
use Symfony\Component\Validator\Constraints\ExpressionValidator;
1717
use Symfony\Component\Validator\Tests\Fixtures\Entity;
18+
use Symfony\Component\Validator\Tests\Fixtures\ToString;
1819
use Symfony\Component\Validator\Validation;
1920

2021
class ExpressionValidatorTest extends AbstractConstraintValidatorTest
@@ -90,6 +91,39 @@ public function testFailingExpressionAtObjectLevel()
9091
->assertRaised();
9192
}
9293

94+
public function testSucceedingExpressionAtObjectLevelWithToString()
95+
{
96+
$constraint = new Expression('this.data == 1');
97+
98+
$object = new ToString();
99+
$object->data = '1';
100+
101+
$this->setObject($object);
102+
103+
$this->validator->validate($object, $constraint);
104+
105+
$this->assertNoViolation();
106+
}
107+
108+
public function testFailingExpressionAtObjectLevelWithToString()
109+
{
110+
$constraint = new Expression(array(
111+
'expression' => 'this.data == 1',
112+
'message' => 'myMessage',
113+
));
114+
115+
$object = new ToString();
116+
$object->data = '2';
117+
118+
$this->setObject($object);
119+
120+
$this->validator->validate($object, $constraint);
121+
122+
$this->buildViolation('myMessage')
123+
->setParameter('{{ value }}', 'toString')
124+
->assertRaised();
125+
}
126+
93127
public function testSucceedingExpressionAtPropertyLevel()
94128
{
95129
$constraint = new Expression('value == this.data');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures;
13+
14+
class ToString
15+
{
16+
public $data;
17+
18+
public function __toString()
19+
{
20+
return 'toString';
21+
}
22+
}

0 commit comments

Comments
 (0)
0