8000 merged branch kriswallsmith/2.2 (PR #6988) · symfony/symfony@bc79f27 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc79f27

Browse files
committed
merged branch kriswallsmith/2.2 (PR #6988)
This PR was merged into the 2.2 branch. Commits ------- da22926 [Validator] gracefully handle transChoice errors Discussion ---------- [Validator] gracefully handle transChoice errors This validator annotation was throwing an error for me: ``` /** @Assert\Length(min=6, minMessage="Must be 6 characters") */ ``` To avoid this error in the current code I would need to provide a message template that accommodates the minimum length being 1, even though that's not the case. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ --------------------------------------------------------------------------- by kriswallsmith at 2013-02-25T19:41:51Z ping @fabpot Any thoughts on this change? Having to provide a transChoice template when you know what the pluralization is going to be is a pain in the neck. --------------------------------------------------------------------------- by kriswallsmith at 2013-02-25T19:42:11Z ping @bschussek too :) --------------------------------------------------------------------------- by fabpot at 2013-02-25T19:57:08Z I'm +1 for this change. What do you think @bschussek? --------------------------------------------------------------------------- by vicb at 2013-02-26T10:44:33Z Would this be a common enough use case to be pushed to the translator ?
2 parents 06560df + da22926 commit bc79f27

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)
0