[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
|
// Only add the error if the form is synchronized
|
||||||
if ($this->acceptsErrors($scope)) {
|
if ($this->acceptsErrors($scope)) {
|
||||||
$scope->addError(new FormError(
|
$scope->addError(new FormError(
|
||||||
|
$violation->getMessage(),
|
||||||
$violation->getMessageTemplate(),
|
$violation->getMessageTemplate(),
|
||||||
$violation->getMessageParameters(),
|
$violation->getMessageParameters(),
|
||||||
$violation->getMessagePluralization()
|
$violation->getMessagePluralization()
|
||||||
|
@ -18,6 +18,11 @@ namespace Symfony\Component\Form;
|
|||||||
*/
|
*/
|
||||||
class FormError
|
class FormError
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The template for the error message
|
* The template for the error message
|
||||||
* @var string
|
* @var string
|
||||||
@ -41,16 +46,19 @@ class FormError
|
|||||||
*
|
*
|
||||||
* Any array key in $messageParameters will be used as a placeholder in
|
* Any array key in $messageParameters will be used as a placeholder in
|
||||||
* $messageTemplate.
|
* $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
|
* @param array $messageParameters The parameters that should be
|
||||||
* substituted in the message template.
|
* substituted in the message template.
|
||||||
* @param integer|null $messagePluralization The value for error message pluralization
|
* @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->messageParameters = $messageParameters;
|
||||||
$this->messagePluralization = $messagePluralization;
|
$this->messagePluralization = $messagePluralization;
|
||||||
}
|
}
|
||||||
@ -62,7 +70,7 @@ class FormError
|
|||||||
*/
|
*/
|
||||||
public function getMessage()
|
public function getMessage()
|
||||||
{
|
{
|
||||||
return strtr($this->messageTemplate, $this->messageParameters);
|
return $this->message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,7 +19,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
|
|||||||
public function testRow()
|
public function testRow()
|
||||||
{
|
{
|
||||||
$form = $this->factory->createNamed('name', 'text');
|
$form = $this->factory->createNamed('name', 'text');
|
||||||
$form->addError(new FormError('Error!'));
|
$form->addError(new FormError('[trans]Error![/trans]'));
|
||||||
$view = $form->createView();
|
$view = $form->createView();
|
||||||
$html = $this->renderRow($view);
|
$html = $this->renderRow($view);
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
|
|||||||
public function testRepeatedRow()
|
public function testRepeatedRow()
|
||||||
{
|
{
|
||||||
$form = $this->factory->createNamed('name', 'repeated');
|
$form = $this->factory->createNamed('name', 'repeated');
|
||||||
$form->addError(new FormError('Error!'));
|
$form->addError(new FormError('[trans]Error![/trans]'));
|
||||||
$view = $form->createView();
|
$view = $form->createView();
|
||||||
$html = $this->renderRow($view);
|
$html = $this->renderRow($view);
|
||||||
|
|
||||||
@ -398,7 +398,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
|
|||||||
)
|
)
|
||||||
->getForm();
|
->getForm();
|
||||||
|
|
||||||
$form->get('child')->addError(new FormError('Error!'));
|
$form->get('child')->addError(new FormError('[trans]Error![/trans]'));
|
||||||
|
|
||||||
$this->assertWidgetMatchesXpath($form->createView(), array(),
|
$this->assertWidgetMatchesXpath($form->createView(), array(),
|
||||||
'/div
|
'/div
|
||||||
|
@ -283,8 +283,8 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
|
|||||||
public function testErrors()
|
public function testErrors()
|
||||||
{
|
{
|
||||||
$form = $this->factory->createNamed('name', 'text');
|
$form = $this->factory->createNamed('name', 'text');
|
||||||
$form->addError(new FormError('Error 1'));
|
$form->addError(new FormError('[trans]Error 1[/trans]'));
|
||||||
$form->addError(new FormError('Error 2'));
|
$form->addError(new FormError('[trans]Error 2[/trans]'));
|
||||||
$view = $form->createView();
|
$view = $form->createView();
|
||||||
$html = $this->renderErrors($view);
|
$html = $this->renderErrors($view);
|
||||||
|
|
||||||
@ -1151,7 +1151,7 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
|
|||||||
{
|
{
|
||||||
$child = $this->factory->createNamed('date', 'date');
|
$child = $this->factory->createNamed('date', 'date');
|
||||||
$form = $this->factory->createNamed('form', 'form')->add($child);
|
$form = $this->factory->createNamed('form', 'form')->add($child);
|
||||||
$child->addError(new FormError('Error!'));
|
$child->addError(new FormError('[trans]Error![/trans]'));
|
||||||
$view = $form->createView();
|
$view = $form->createView();
|
||||||
|
|
||||||
$this->assertEmpty($this->renderErrors($view));
|
$this->assertEmpty($this->renderErrors($view));
|
||||||
@ -1676,7 +1676,7 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
|
|||||||
{
|
{
|
||||||
$child = $this->factory->createNamed('time', 'time');
|
$child = $this->factory->createNamed('time', 'time');
|
||||||
$form = $this->factory->createNamed('form', 'form')->add($child);
|
$form = $this->factory->createNamed('form', 'form')->add($child);
|
||||||
$child->addError(new FormError('Error!'));
|
$child->addError(new FormError('[trans]Error![/trans]'));
|
||||||
$view = $form->createView();
|
$view = $form->createView();
|
||||||
|
|
||||||
$this->assertEmpty($this->renderErrors($view));
|
$this->assertEmpty($this->renderErrors($view));
|
||||||
|
@ -18,7 +18,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest
|
|||||||
public function testRow()
|
public function testRow()
|
||||||
{
|
{
|
||||||
$form = $this->factory->createNamed('name', 'text');
|
$form = $this->factory->createNamed('name', 'text');
|
||||||
$form->addError(new FormError('Error!'));
|
$form->addError(new FormError('[trans]Error![/trans]'));
|
||||||
$view = $form->createView();
|
$view = $form->createView();
|
||||||
$html = $this->renderRow($view);
|
$html = $this->renderRow($view);
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest
|
|||||||
public function testRepeatedRowWithErrors()
|
public function testRepeatedRowWithErrors()
|
||||||
{
|
{
|
||||||
$form = $this->factory->createNamed('name', 'repeated');
|
$form = $this->factory->createNamed('name', 'repeated');
|
||||||
$form->addError(new FormError('Error!'));
|
$form->addError(new FormError('[trans]Error![/trans]'));
|
||||||
$view = $form->createView();
|
$view = $form->createView();
|
||||||
$html = $this->renderRow($view);
|
$html = $this->renderRow($view);
|
||||||
|
|
||||||
@ -250,7 +250,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest
|
|||||||
)
|
)
|
||||||
->getForm();
|
->getForm();
|
||||||
|
|
||||||
$form->get('child')->addError(new FormError('Error!'));
|
$form->get('child')->addError(new FormError('[trans]Error![/trans]'));
|
||||||
|
|
||||||
$this->assertWidgetMatchesXpath($form->createView(), array(),
|
$this->assertWidgetMatchesXpath($form->createView(), array(),
|
||||||
'/table
|
'/table
|
||||||
|
@ -49,6 +49,8 @@ class ValidationListenerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
private $message;
|
private $message;
|
||||||
|
|
||||||
|
private $messageTemplate;
|
||||||
|
|
||||||
private $params;
|
private $params;
|
||||||
|
|
||||||
protected function setUp()
|
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->violationMapper = $this->getMock('Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface');
|
||||||
$this->listener = new ValidationListener($this->validator, $this->violationMapper);
|
$this->listener = new ValidationListener($this->validator, $this->violationMapper);
|
||||||
$this->message = 'Message';
|
$this->message = 'Message';
|
||||||
|
$this->messageTemplate = 'Message template';
|
||||||
$this->params = array('foo' => 'bar');
|
$this->params = array('foo' => 'bar');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getConstraintViolation($code = null)
|
private function getConstraintViolation($code = null)
|
||||||
{
|
{
|
||||||
return new ConstraintViolation($this->message, $this->params, null, 'prop.path', null, null, $code);
|
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code);
|
||||||
}
|
|
||||||
|
|
||||||
private function getFormError()
|
|
||||||
{
|
|
||||||
return new FormError($this->message, $this->params);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
|
private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
|
||||||
|
@ -48,6 +48,11 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
private $message;
|
private $message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $messageTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
@ -62,6 +67,7 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||||
$this->mapper = new ViolationMapper();
|
$this->mapper = new ViolationMapper();
|
||||||
$this->message = 'Message';
|
$this->message = 'Message';
|
||||||
|
$this->messageTemplate = 'Message template';
|
||||||
$this->params = array('foo' => 'bar');
|
$this->params = array('foo' => 'bar');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +107,7 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
protected function getConstraintViolation($propertyPath)
|
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()
|
protected function getFormError()
|
||||||
{
|
{
|
||||||
return new FormError($this->message, $this->params);
|
return new FormError($this->message, $this->messageTemplate, $this->params);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMapToVirtualFormIfDataDoesNotMatch()
|
public function testMapToVirtualFormIfDataDoesNotMatch()
|
||||||
|
Reference in New Issue
Block a user