[Form] Adapted Form component to translator integration in the validator
This commit is contained in:
parent
46f751ccf2
commit
e7eb5b0d7d
|
@ -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()
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
Reference in New Issue