Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da22926

Browse files
committed
[Validator] gracefully handle transChoice errors
1 parent 1f68f40 commit da22926

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Symfony/Component/Validator/ExecutionContext.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,18 @@ public function __construct(GlobalExecutionContextInterface $globalContext, Tran
8989
*/
9090
public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
9191
{
92+
if (null === $pluralization) {
93+
$translatedMessage = $this->translator->trans($message, $params, $this->translationDomain);
94+
} else {
95+
try {
96+
$translatedMessage = $this->translator->transChoice($message, $pluralization, $params, $this->translationDomain);
97+
} catch (\InvalidArgumentException $e) {
98+
$translatedMessage = $this->translator->trans($message, $params, $this->translationDomain);
99+
}
100+
}
101+
92102
$this->globalContext->getViolations()->add(new ConstraintViolation(
93-
null === $pluralization
94-
? $this->translator->trans($message, $params, $this->translationDomain)
95-
: $this->translator->transChoice($message, $pluralization, $params, $this->translationDomain),
103+
$translatedMessage,
96104
$message,
97105
$params,
98106
$this->globalContext->getRoot(),

src/Symfony/Component/Validator/Tests/ExecutionContextTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,19 @@ public function testAddViolationAtUsesPassedNullValue()
376376
)), $this->context->getViolations());
377377
}
378378

379+
public function testAddViolationPluralTranslationError()
380+
{
381+
$this->translator->expects($this->once())
382+
->method('transChoice')
383+
->with('foo')
384+
->will($this->throwException(new \InvalidArgumentException()));
385+
$this->translator->expects($this->once())
386+
->method('trans')
387+
->with('foo');
388+
389+
$this->context->addViolation('foo', array(), null, 2);
390+
}
391+
379392
public function testGetPropertyPath()
380393
{
381394
$this->assertEquals('foo.bar', $this->context->getPropertyPath());

0 commit comments

Comments
 (0)