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 ?
This commit is contained in:
commit
bc79f27f70
@ -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(),
|
||||
|
@ -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());
|
||||
|
Reference in New Issue
Block a user