From da2292681fb1d67961f0cd41a93be131e1b7392f Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Wed, 6 Feb 2013 12:54:30 -0800 Subject: [PATCH] [Validator] gracefully handle transChoice errors --- .../Component/Validator/ExecutionContext.php | 14 +++++++++++--- .../Validator/Tests/ExecutionContextTest.php | 13 +++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/ExecutionContext.php b/src/Symfony/Component/Validator/ExecutionContext.php index 864f749da83c4..4e904d158fc68 100644 --- a/src/Symfony/Component/Validator/ExecutionContext.php +++ b/src/Symfony/Component/Validator/ExecutionContext.php @@ -89,10 +89,18 @@ public function __construct(GlobalExecutionContextInterface $globalContext, Tran */ 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 9283aa6776df5..0cd92f711f71c 100644 --- a/src/Symfony/Component/Validator/Tests/ExecutionContextTest.php +++ b/src/Symfony/Component/Validator/Tests/ExecutionContextTest.php @@ -376,6 +376,19 @@ public function testAddViolationAtUsesPassedNullValue() )), $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());