8000 [Form] Reintroduced the option "invalid_message_parameters" · lchenay/symfony@0add23f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0add23f

Browse files
committed
[Form] Reintroduced the option "invalid_message_parameters"
1 parent 50df1a7 commit 0add23f

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function validate($form, Constraint $constraint)
8787
// Mark the form with an error if it is not synchronized
8888
$this->context->addViolation(
8989
$config->getOption('invalid_message'),
90-
array('{{ value }}' => $clientDataAsString),
90+
array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')),
9191
$form->getViewData(),
9292
null,
9393
Form::ERR_INVALID

src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
7777
};
7878

7979
$resolver->setDefaults(array(
80-
'error_mapping' => array(),
81-
'validation_groups' => null,
80+
'error_mapping' => array(),
81+
'validation_groups' => null,
8282
// "validation_constraint" is deprecated. Use "constraints".
83-
'validation_constraint' => null,
84-
'constraints' => $constraints,
85-
'cascade_validation' => false,
86-
'invalid_message' => 'This value is not valid.',
87-
'extra_fields_message' => 'This form should not contain extra fields.',
88-
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
83+
'validation_constraint' => null,
84+
'constraints' => $constraints,
85+
'cascade_validation' => false,
86+
'invalid_message' => 'This value is not valid.',
87+
'invalid_message_parameters' => array(),
88+
'extra_fields_message' => 'This form should not contain extra fields.',
89+
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
8990
));
9091

9192
$resolver->setNormalizers(array(

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Validator\Constraints;
1313

1414
use Symfony\Component\Form\FormBuilder;
15+
use Symfony\Component\Validator\ConstraintViolation;
1516
use Symfony\Component\Form\Exception\TransformationFailedException;
1617
use Symfony\Component\Form\CallbackTransformer;
1718
use Symfony\Component\Form\FormInterface;
@@ -182,7 +183,13 @@ public function testDontValidateIfNotSynchronized()
182183
$graphWalker = $context->getGraphWalker();
183184
$object = $this->getMock('\stdClass');
184185

185-
$form = $this->getBuilder('name', '\stdClass', array('invalid_message' => 'Invalid!'))
186+
$form = $this->getBuilder('name', '\stdClass', array(
187+
'invalid_message' => 'invalid_message_key',
188+
// Invalid message parameters must be supported, because the
189+
// invalid message can be a translation key
190+
// see https://github.com/symfony/symfony/issues/5144
191+
'invalid_message_parameters' => array('{{ foo }}' => 'bar'),
192+
))
186193
->setData($object)
187194
->addViewTransformer(new CallbackTransformer(
188195
function ($data) { return $data; },
@@ -191,16 +198,26 @@ function () { throw new TransformationFailedException(); }
191198
->getForm();
192199

193200
// Launch transformer
194-
$form->bind(array());
201+
$form->bind('foo');
195202

196203
$graphWalker->expects($this->never())
197204
->method('walkReference');
198205

199206
$this->validator->initialize($context);
200207
$this->validator->validate($form, new Form());
201208

209+
$expectedViolation = new ConstraintViolation(
210+
'invalid_message_key',
211+
array('{{ value }}' => 'foo', '{{ foo }}' => 'bar'),
212+
'Root',
213+
null,
214+
'foo',
215+
null,
216+
Form::ERR_INVALID
217+
);
218+
202219
$this->assertCount(1, $context->getViolations());
203-
$this->assertEquals('Invalid!', $context->getViolations()->get(0)->getMessage());
220+
$this->assertEquals($expectedViolation, $context->getViolations()->get(0));
204221
}
205222

206223
public function testDontValidateConstraintsIfNotSynchronized()
@@ -517,6 +534,7 @@ private function getBuilder($name = 'name', $dataClass = null, array $options =
517534
{
518535
$options = array_replace(array(
519536
'constraints' => array(),
537+
'invalid_message_parameters' => array(),
520538
), $options);
521539

522540
return new FormBuilder($name, $dataClass, $this->dispatcher, $this->factory, $options);

0 commit comments

Comments
 (0)
0