Skip to content

Commit d3f8bf1

Browse files
Merge branch '2.7' into 2.8
* 2.7: [FrameworkBundle] Fix visibility of a test helper [link] clear the cache after linking [link] Prevent warnings when running link with 2.7 [Validator] ExpressionValidator should use OBJECT_TO_STRING to allow value in message
2 parents 497b140 + 4d78631 commit d3f8bf1

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

Constraints/ExpressionValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public function validate($value, Constraint $constraint)
7272
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
7373
if ($this->context instanceof ExecutionContextInterface) {
7474
$this->context->buildViolation($constraint->message)
75-
->setParameter('{{ value }}', $this->formatValue($value))
75+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
7676
->setCode(Expression::EXPRESSION_FAILED_ERROR)
7777
->addViolation();
7878
} else {
7979
$this->buildViolation($constraint->message)
80-
->setParameter('{{ value }}', $this->formatValue($value))
80+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
8181
->setCode(Expression::EXPRESSION_FAILED_ERROR)
8282
->addViolation();
8383
}

Tests/Constraints/ExpressionValidatorTest.php

Lines changed: 35 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
@@ -93,6 +94,40 @@ public function testFailingExpressionAtObjectLevel()
9394
->assertRaised();
9495
}
9596

97+
public function testSucceedingExpressionAtObjectLevelWithToString()
98+
{
99+
$constraint = new Expression('this.data == 1');
100+
101+
$object = new ToString();
102+
$object->data = '1';
103+
104+
$this->setObject($object);
105+
106+
$this->validator->validate($object, $constraint);
107+
108+
$this->assertNoViolation();
109+
}
110+
111+
public function testFailingExpressionAtObjectLevelWithToString()
112+
{
113+
$constraint = new Expression(array(
114+
'expression' => 'this.data == 1',
115+
'message' => 'myMessage',
116+
));
117+
118+
$object = new ToString();
119+
$object->data = '2';
120+
121+
$this->setObject($object);
122+
123+
$this->validator->validate($object, $constraint);
124+
125+
$this->buildViolation('myMessage')
126+
->setParameter('{{ value }}', 'toString')
127+
->setCode(Expression::EXPRESSION_FAILED_ERROR)
128+
->assertRaised();
129+
}
130+
96131
public function testSucceedingExpressionAtPropertyLevel()
97132
{
98133
$constraint = new Expression('value == this.data');

Tests/Fixtures/ToString.php

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)