diff --git a/src/Symfony/Component/Validator/ExecutionContext.php b/src/Symfony/Component/Validator/ExecutionContext.php index 864f749da8..4e904d158f 100644 --- a/src/Symfony/Component/Validator/ExecutionContext.php +++ b/src/Symfony/Component/Validator/ExecutionContext.php @@ -89,10 +89,18 @@ class ExecutionContext implements ExecutionContextInterface */ public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null) { + if (null === $pluralization) { + $translatedMessage = $this->translator->trans($message, $params, $this->translationDomain); + } else { + try { + $translatedMessage = $this->translator->transChoice($message, $pluralization, $params, $this->translationDomain); + } catch (\InvalidArgumentException $e) { + $translatedMessage = $this->translator->trans($message, $params, $this->translationDomain); + } + } + $this->globalContext->getViolations()->add(new ConstraintViolation( - null === $pluralization - ? $this->translator->trans($message, $params, $this->translationDomain) - : $this->translator->transChoice($message, $pluralization, $params, $this->translationDomain), + $translatedMessage, $message, $params, $this->globalContext->getRoot(), diff --git a/src/Symfony/Component/Validator/Tests/ExecutionContextTest.php b/src/Symfony/Component/Validator/Tests/ExecutionContextTest.php index 9283aa6776..0cd92f711f 100644 --- a/src/Symfony/Component/Validator/Tests/ExecutionContextTest.php +++ b/src/Symfony/Component/Validator/Tests/ExecutionContextTest.php @@ -376,6 +376,19 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase )), $this->context->getViolations()); } + public function testAddViolationPluralTranslationError() + { + $this->translator->expects($this->once()) + ->method('transChoice') + ->with('foo') + ->will($this->throwException(new \InvalidArgumentException())); + $this->translator->expects($this->once()) + ->method('trans') + ->with('foo'); + + $this->context->addViolation('foo', array(), null, 2); + } + public function testGetPropertyPath() { $this->assertEquals('foo.bar', $this->context->getPropertyPath());