Skip to content

Commit f07dbc6

Browse files
author
Amrouche Hamza
committed
[Validator] ExpressionValidator should use OBJECT_TO_STRING to allow value in message
1 parent a522e04 commit f07dbc6

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/ProfilerPassTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function testValidCollector()
6161
->method('getDefinition');
6262

6363
// assert that the data_collector.templates parameter should be set
64+
$container->expects($this->once())
65+
->method('setParameter')
66+
->with('framework.translator.logging', false);
67+
6468
$container->expects($this->once())
6569
->method('setParameter')
6670
->with('data_collector.templates', array('my_collector_service' => array('my_collector', 'foo')));

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected static function getBundleDefaultConfig()
152152
'translator' => array(
153153
'enabled' => !class_exists(FullStack::class),
154154
'fallbacks' => array('en'),
155-
'logging' => true,
155+
'logging' => false,
156156
'formatter' => 'translator.formatter.default',
157157
'paths' => array(),
158158
),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function validate($value, Constraint $constraint)
4848

4949
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
5050
$this->context->buildViolation($constraint->message)
51-
->setParameter('{{ value }}', $this->formatValue($value))
51+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
5252
->setCode(Expression::EXPRESSION_FAILED_ERROR)
5353
->addViolation();
5454
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraints\ExpressionValidator;
1616
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
1717
use Symfony\Component\Validator\Tests\Fixtures\Entity;
18+
use Symfony\Component\Validator\Tests\Fixtures\ToString;
1819

1920
class ExpressionValidatorTest extends ConstraintValidatorTestCase
2021
{
@@ -87,6 +88,40 @@ public function testFailingExpressionAtObjectLevel()
8788
->assertRaised();
8889
}
8990

91+
public function testSucceedingExpressionAtObjectLevelWithToString()
92+
{
93+
$constraint = new Expression('this.data == 1');
94+
95+
$object = new ToString();
96+
$object->data = '1';
97+
98+
$this->setObject($object);
99+
100+
$this->validator->validate($object, $constraint);
101+
102+
$this->assertNoViolation();
103+
}
104+
105+
public function testFailingExpressionAtObjectLevelWithToString()
106+
{
107+
$constraint = new Expression(array(
108+
'expression' => 'this.data == 1',
109+
'message' => 'myMessage',
110+
));
111+
112+
$object = new ToString();
113+
$object->data = '2';
114+
115+
$this->setObject($object);
116+
117+
$this->validator->validate($object, $constraint);
118+
119+
$this->buildViolation('myMessage')
120+
->setParameter('{{ value }}', 'toString')
121+
->setCode(Expression::EXPRESSION_FAILED_ERROR)
122+
->assertRaised();
123+
}
124+
90125
public function testSucceedingExpressionAtPropertyLevel()
91126
{
92127
$constraint = new Expression('value == this.data');
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)