diff --git a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php index 9ac6bb69b3..cece98804e 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php +++ b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php @@ -123,6 +123,7 @@ class ViolationMapper implements ViolationMapperInterface // Only add the error if the form is synchronized if ($this->acceptsErrors($scope)) { $scope->addError(new FormError( + $violation->getMessage(), $violation->getMessageTemplate(), $violation->getMessageParameters(), $violation->getMessagePluralization() diff --git a/src/Symfony/Component/Form/FormError.php b/src/Symfony/Component/Form/FormError.php index b336a40388..343165ca46 100644 --- a/src/Symfony/Component/Form/FormError.php +++ b/src/Symfony/Component/Form/FormError.php @@ -18,6 +18,11 @@ namespace Symfony\Component\Form; */ class FormError { + /** + * @var string + */ + private $message; + /** * The template for the error message * @var string @@ -41,16 +46,19 @@ class FormError * * Any array key in $messageParameters will be used as a placeholder in * $messageTemplate. - * @see \Symfony\Component\Translation\Translator * - * @param string $messageTemplate The template for the error message + * @param string $message The translated error message + * @param string|null $messageTemplate The template for the error message * @param array $messageParameters The parameters that should be * substituted in the message template. * @param integer|null $messagePluralization The value for error message pluralization + * + * @see \Symfony\Component\Translation\Translator */ - public function __construct($messageTemplate, array $messageParameters = array(), $messagePluralization = null) + public function __construct($message, $messageTemplate = null, array $messageParameters = array(), $messagePluralization = null) { - $this->messageTemplate = $messageTemplate; + $this->message = $message; + $this->messageTemplate = $messageTemplate ?: $message; $this->messageParameters = $messageParameters; $this->messagePluralization = $messagePluralization; } @@ -62,7 +70,7 @@ class FormError */ public function getMessage() { - return strtr($this->messageTemplate, $this->messageParameters); + return $this->message; } /** diff --git a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php index ac9b18b065..7fd743006e 100644 --- a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php @@ -19,7 +19,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest public function testRow() { $form = $this->factory->createNamed('name', 'text'); - $form->addError(new FormError('Error!')); + $form->addError(new FormError('[trans]Error![/trans]')); $view = $form->createView(); $html = $this->renderRow($view); @@ -58,7 +58,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest public function testRepeatedRow() { $form = $this->factory->createNamed('name', 'repeated'); - $form->addError(new FormError('Error!')); + $form->addError(new FormError('[trans]Error![/trans]')); $view = $form->createView(); $html = $this->renderRow($view); @@ -398,7 +398,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest ) ->getForm(); - $form->get('child')->addError(new FormError('Error!')); + $form->get('child')->addError(new FormError('[trans]Error![/trans]')); $this->assertWidgetMatchesXpath($form->createView(), array(), '/div diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index a7ff80e9de..e2801c2d8e 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -283,8 +283,8 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase public function testErrors() { $form = $this->factory->createNamed('name', 'text'); - $form->addError(new FormError('Error 1')); - $form->addError(new FormError('Error 2')); + $form->addError(new FormError('[trans]Error 1[/trans]')); + $form->addError(new FormError('[trans]Error 2[/trans]')); $view = $form->createView(); $html = $this->renderErrors($view); @@ -1151,7 +1151,7 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase { $child = $this->factory->createNamed('date', 'date'); $form = $this->factory->createNamed('form', 'form')->add($child); - $child->addError(new FormError('Error!')); + $child->addError(new FormError('[trans]Error![/trans]')); $view = $form->createView(); $this->assertEmpty($this->renderErrors($view)); @@ -1676,7 +1676,7 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase { $child = $this->factory->createNamed('time', 'time'); $form = $this->factory->createNamed('form', 'form')->add($child); - $child->addError(new FormError('Error!')); + $child->addError(new FormError('[trans]Error![/trans]')); $view = $form->createView(); $this->assertEmpty($this->renderErrors($view)); diff --git a/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php index efa957fb0b..e0f62c4ab1 100644 --- a/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php @@ -18,7 +18,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest public function testRow() { $form = $this->factory->createNamed('name', 'text'); - $form->addError(new FormError('Error!')); + $form->addError(new FormError('[trans]Error![/trans]')); $view = $form->createView(); $html = $this->renderRow($view); @@ -91,7 +91,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest public function testRepeatedRowWithErrors() { $form = $this->factory->createNamed('name', 'repeated'); - $form->addError(new FormError('Error!')); + $form->addError(new FormError('[trans]Error![/trans]')); $view = $form->createView(); $html = $this->renderRow($view); @@ -250,7 +250,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest ) ->getForm(); - $form->get('child')->addError(new FormError('Error!')); + $form->get('child')->addError(new FormError('[trans]Error![/trans]')); $this->assertWidgetMatchesXpath($form->createView(), array(), '/table diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php index 7e358910e2..d9555e13e1 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php @@ -49,6 +49,8 @@ class ValidationListenerTest extends \PHPUnit_Framework_TestCase private $message; + private $messageTemplate; + private $params; protected function setUp() @@ -63,17 +65,13 @@ class ValidationListenerTest extends \PHPUnit_Framework_TestCase $this->violationMapper = $this->getMock('Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface'); $this->listener = new ValidationListener($this->validator, $this->violationMapper); $this->message = 'Message'; + $this->messageTemplate = 'Message template'; $this->params = array('foo' => 'bar'); } private function getConstraintViolation($code = null) { - return new ConstraintViolation($this->message, $this->params, null, 'prop.path', null, null, $code); - } - - private function getFormError() - { - return new FormError($this->message, $this->params); + return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code); } private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null) diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php index e192f07108..2f41a99101 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php @@ -48,6 +48,11 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase */ private $message; + /** + * @var string + */ + private $messageTemplate; + /** * @var array */ @@ -62,6 +67,7 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); $this->mapper = new ViolationMapper(); $this->message = 'Message'; + $this->messageTemplate = 'Message template'; $this->params = array('foo' => 'bar'); } @@ -101,7 +107,7 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase */ protected function getConstraintViolation($propertyPath) { - return new ConstraintViolation($this->message, $this->params, null, $propertyPath, null); + return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, $propertyPath, null); } /** @@ -109,7 +115,7 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase */ protected function getFormError() { - return new FormError($this->message, $this->params); + return new FormError($this->message, $this->messageTemplate, $this->params); } public function testMapToVirtualFormIfDataDoesNotMatch()