Skip to content

Commit e7eb5b0

Browse files
committed
[Form] Adapted Form component to translator integration in the validator
1 parent 46f751c commit e7eb5b0

File tree

7 files changed

+36
-23
lines changed

7 files changed

+36
-23
lines changed

src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form
123123
// Only add the error if the form is synchronized
124124
if ($this->acceptsErrors($scope)) {
125125
$scope->addError(new FormError(
126+
$violation->getMessage(),
126127
$violation->getMessageTemplate(),
127128
$violation->getMessageParameters(),
128129
$violation->getMessagePluralization()

src/Symfony/Component/Form/FormError.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
class FormError
2020
{
21+
/**
22+
* @var string
23+
*/
24+
private $message;
25+
2126
/**
2227
* The template for the error message
2328
* @var string
@@ -41,16 +46,19 @@ class FormError
4146
*
4247
* Any array key in $messageParameters will be used as a placeholder in
4348
* $messageTemplate.
44-
* @see \Symfony\Component\Translation\Translator
4549
*
46-
* @param string $messageTemplate The template for the error message
50+
* @param string $message The translated error message
51+
* @param string|null $messageTemplate The template for the error message
4752
* @param array $messageParameters The parameters that should be
4853
* substituted in the message template.
4954
* @param integer|null $messagePluralization The value for error message pluralization
55+
*
56+
* @see \Symfony\Component\Translation\Translator
5057
*/
51-
public function __construct($messageTemplate, array $messageParameters = array(), $messagePluralization = null)
58+
public function __construct($message, $messageTemplate = null, array $messageParameters = array(), $messagePluralization = null)
5259
{
53-
$this->messageTemplate = $messageTemplate;
60+
$this->message = $message;
61+
$this->messageTemplate = $messageTemplate ?: $message;
5462
$this->messageParameters = $messageParameters;
5563
$this->messagePluralization = $messagePluralization;
5664
}
@@ -62,7 +70,7 @@ public function __construct($messageTemplate, array $messageParameters = array()
6270
*/
6371
public function getMessage()
6472
{
65-
return strtr($this->messageTemplate, $this->messageParameters);
73+
return $this->message;
6674
}
6775

6876
/**

src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
1919
public function testRow()
2020
{
2121
$form = $this->factory->createNamed('name', 'text');
22-
$form->addError(new FormError('Error!'));
22+
$form->addError(new FormError('[trans]Error![/trans]'));
2323
$view = $form->createView();
2424
$html = $this->renderRow($view);
2525

@@ -58,7 +58,7 @@ public function testRowOverrideVariables()
5858
public function testRepeatedRow()
5959
{
6060
$form = $this->factory->createNamed('name', 'repeated');
61-
$form->addError(new FormError('Error!'));
61+
$form->addError(new FormError('[trans]Error![/trans]'));
6262
$view = $form->createView();
6363
$html = $this->renderRow($view);
6464

@@ -398,7 +398,7 @@ public function testNestedFormError()
398398
)
399399
->getForm();
400400

401-
$form->get('child')->addError(new FormError('Error!'));
401+
$form->get('child')->addError(new FormError('[trans]Error![/trans]'));
402402

403403
$this->assertWidgetMatchesXpath($form->createView(), array(),
404404
'/div

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
283283
public function testErrors()
284284
{
285285
$form = $this->factory->createNamed('name', 'text');
286-
$form->addError(new FormError('Error 1'));
287-
$form->addError(new FormError('Error 2'));
286+
$form->addError(new FormError('[trans]Error 1[/trans]'));
287+
$form->addError(new FormError('[trans]Error 2[/trans]'));
288288
$view = $form->createView();
289289
$html = $this->renderErrors($view);
290290

@@ -1151,7 +1151,7 @@ public function testDateErrorBubbling()
11511151
{
11521152
$child = $this->factory->createNamed('date', 'date');
11531153
$form = $this->factory->createNamed('form', 'form')->add($child);
1154-
$child->addError(new FormError('Error!'));
1154+
$child->addError(new FormError('[trans]Error![/trans]'));
11551155
$view = $form->createView();
11561156

11571157
$this->assertEmpty($this->renderErrors($view));
@@ -1676,7 +1676,7 @@ public function testTimeErrorBubbling()
16761676
{
16771677
$child = $this->factory->createNamed('time', 'time');
16781678
$form = $this->factory->createNamed('form', 'form')->add($child);
1679-
$child->addError(new FormError('Error!'));
1679+
$child->addError(new FormError('[trans]Error![/trans]'));
16801680
$view = $form->createView();
16811681

16821682
$this->assertEmpty($this->renderErrors($view));

src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest
1818
public function testRow()
1919
{
2020
$form = $this->factory->createNamed('name', 'text');
21-
$form->addError(new FormError('Error!'));
21+
$form->addError(new FormError('[trans]Error![/trans]'));
2222
$view = $form->createView();
2323
$html = $this->renderRow($view);
2424

@@ -91,7 +91,7 @@ public function testRepeatedRow()
9191
public function testRepeatedRowWithErrors()
9292
{
9393
$form = $this->factory->createNamed('name', 'repeated');
94-
$form->addError(new FormError('Error!'));
94+
$form->addError(new FormError('[trans]Error![/trans]'));
9595
$view = $form->createView();
9696
$html = $this->renderRow($view);
9797

@@ -250,7 +250,7 @@ public function testNestedFormError()
250250
)
251251
->getForm();
252252

253-
$form->get('child')->addError(new FormError('Error!'));
253+
$form->get('child')->addError(new FormError('[trans]Error![/trans]'));
254254

255255
$this->assertWidgetMatchesXpath($form->createView(), array(),
256256
'/table

src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class ValidationListenerTest extends \PHPUnit_Framework_TestCase
4949

5050
private $message;
5151

52+
private $messageTemplate;
53+
5254
private $params;
5355

5456
protected function setUp()
@@ -63,17 +65,13 @@ protected function setUp()
6365
$this->violationMapper = $this->getMock('Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface');
6466
$this->listener = new ValidationListener($this->validator, $this->violationMapper);
6567
$this->message = 'Message';
68+
$this->messageTemplate = 'Message template';
6669
$this->params = array('foo' => 'bar');
6770
}
6871

6972
private function getConstraintViolation($code = null)
7073
{
71-
return new ConstraintViolation($this->message, $this->params, null, 'prop.path', null, null, $code);
72-
}
73-
74-
private function getFormError()
75-
{
76-
return new FormError($this->message, $this->params);
74+
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code);
7775
}
7876

7977
private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)

src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase
4848
*/
4949
private $message;
5050

51+
/**
52+
* @var string
53+
*/
54+
private $messageTemplate;
55+
5156
/**
5257
* @var array
5358
*/
@@ -62,6 +67,7 @@ protected function setUp()
6267
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
6368
$this->mapper = new ViolationMapper();
6469
$this->message = 'Message';
70+
$this->messageTemplate = 'Message template';
6571
$this->params = array('foo' => 'bar');
6672
}
6773

@@ -101,15 +107,15 @@ private function getDataMapper()
101107
*/
102108
protected function getConstraintViolation($propertyPath)
103109
{
104-
return new ConstraintViolation($this->message, $this->params, null, $propertyPath, null);
110+
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, $propertyPath, null);
105111
}
106112

107113
/**
108114
* @return FormError
109115
*/
110116
protected function getFormError()
111117
{
112-
return new FormError($this->message, $this->params);
118+
return new FormError($this->message, $this->messageTemplate, $this->params);
113119
}
114120

115121
public function testMapToVirtualFormIfDataDoesNotMatch()

0 commit comments

Comments
 (0)