Skip to content

Commit

Permalink
bug #24750 [Validator] ExpressionValidator should use OBJECT_TO_STRIN…
Browse files Browse the repository at this point in the history
…G (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 | #24595
| License       | MIT
| Doc PR        | Will do.

Commits
-------

7dac528 [Validator] ExpressionValidator should use OBJECT_TO_STRING to allow value in message
  • Loading branch information
fabpot committed Dec 1, 2017
2 parents e2c608a + 7dac528 commit 5a6e2d7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
Expand Up @@ -78,11 +78,11 @@ public function validate($value, Constraint $constraint)
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
->addViolation();
}
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Validator\Constraints\Expression;
use Symfony\Component\Validator\Constraints\ExpressionValidator;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Symfony\Component\Validator\Tests\Fixtures\ToString;
use Symfony\Component\Validator\Validation;

class ExpressionValidatorTest extends AbstractConstraintValidatorTest
Expand Down Expand Up @@ -90,6 +91,39 @@ public function testFailingExpressionAtObjectLevel()
->assertRaised();
}

public function testSucceedingExpressionAtObjectLevelWithToString()
{
$constraint = new Expression('this.data == 1');

$object = new ToString();
$object->data = '1';

$this->setObject($object);

$this->validator->validate($object, $constraint);

$this->assertNoViolation();
}

public function testFailingExpressionAtObjectLevelWithToString()
{
$constraint = new Expression(array(
'expression' => 'this.data == 1',
'message' => 'myMessage',
));

$object = new ToString();
$object->data = '2';

$this->setObject($object);

$this->validator->validate($object, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'toString')
->assertRaised();
}

public function testSucceedingExpressionAtPropertyLevel()
{
$constraint = new Expression('value == this.data');
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/ToString.php
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

class ToString
{
public $data;

public function __toString()
{
return 'toString';
}
}

0 comments on commit 5a6e2d7

Please sign in to comment.