[Form] Reintroduced the option "invalid_message_parameters"
This commit is contained in:
parent
50df1a72aa
commit
0add23f8d6
@ -87,7 +87,7 @@ class FormValidator extends ConstraintValidator
|
|||||||
// Mark the form with an error if it is not synchronized
|
// Mark the form with an error if it is not synchronized
|
||||||
$this->context->addViolation(
|
$this->context->addViolation(
|
||||||
$config->getOption('invalid_message'),
|
$config->getOption('invalid_message'),
|
||||||
array('{{ value }}' => $clientDataAsString),
|
array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')),
|
||||||
$form->getViewData(),
|
$form->getViewData(),
|
||||||
null,
|
null,
|
||||||
Form::ERR_INVALID
|
Form::ERR_INVALID
|
||||||
|
@ -77,15 +77,16 @@ class FormTypeValidatorExtension extends AbstractTypeExtension
|
|||||||
};
|
};
|
||||||
|
|
||||||
$resolver->setDefaults(array(
|
$resolver->setDefaults(array(
|
||||||
'error_mapping' => array(),
|
'error_mapping' => array(),
|
||||||
'validation_groups' => null,
|
'validation_groups' => null,
|
||||||
// "validation_constraint" is deprecated. Use "constraints".
|
// "validation_constraint" is deprecated. Use "constraints".
|
||||||
'validation_constraint' => null,
|
'validation_constraint' => null,
|
||||||
'constraints' => $constraints,
|
'constraints' => $constraints,
|
||||||
'cascade_validation' => false,
|
'cascade_validation' => false,
|
||||||
'invalid_message' => 'This value is not valid.',
|
'invalid_message' => 'This value is not valid.',
|
||||||
'extra_fields_message' => 'This form should not contain extra fields.',
|
'invalid_message_parameters' => array(),
|
||||||
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
|
'extra_fields_message' => 'This form should not contain extra fields.',
|
||||||
|
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setNormalizers(array(
|
$resolver->setNormalizers(array(
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Form\Tests\Extension\Validator\Constraints;
|
namespace Symfony\Component\Form\Tests\Extension\Validator\Constraints;
|
||||||
|
|
||||||
use Symfony\Component\Form\FormBuilder;
|
use Symfony\Component\Form\FormBuilder;
|
||||||
|
use Symfony\Component\Validator\ConstraintViolation;
|
||||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||||
use Symfony\Component\Form\CallbackTransformer;
|
use Symfony\Component\Form\CallbackTransformer;
|
||||||
use Symfony\Component\Form\FormInterface;
|
use Symfony\Component\Form\FormInterface;
|
||||||
@ -182,7 +183,13 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$graphWalker = $context->getGraphWalker();
|
$graphWalker = $context->getGraphWalker();
|
||||||
$object = $this->getMock('\stdClass');
|
$object = $this->getMock('\stdClass');
|
||||||
|
|
||||||
$form = $this->getBuilder('name', '\stdClass', array('invalid_message' => 'Invalid!'))
|
$form = $this->getBuilder('name', '\stdClass', array(
|
||||||
|
'invalid_message' => 'invalid_message_key',
|
||||||
|
// Invalid message parameters must be supported, because the
|
||||||
|
// invalid message can be a translation key
|
||||||
|
// see https://github.com/symfony/symfony/issues/5144
|
||||||
|
'invalid_message_parameters' => array('{{ foo }}' => 'bar'),
|
||||||
|
))
|
||||||
->setData($object)
|
->setData($object)
|
||||||
->addViewTransformer(new CallbackTransformer(
|
->addViewTransformer(new CallbackTransformer(
|
||||||
function ($data) { return $data; },
|
function ($data) { return $data; },
|
||||||
@ -191,7 +198,7 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->getForm();
|
->getForm();
|
||||||
|
|
||||||
// Launch transformer
|
// Launch transformer
|
||||||
$form->bind(array());
|
$form->bind('foo');
|
||||||
|
|
||||||
$graphWalker->expects($this->never())
|
$graphWalker->expects($this->never())
|
||||||
->method('walkReference');
|
->method('walkReference');
|
||||||
@ -199,8 +206,18 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->validator->initialize($context);
|
$this->validator->initialize($context);
|
||||||
$this->validator->validate($form, new Form());
|
$this->validator->validate($form, new Form());
|
||||||
|
|
||||||
|
$expectedViolation = new ConstraintViolation(
|
||||||
|
'invalid_message_key',
|
||||||
|
array('{{ value }}' => 'foo', '{{ foo }}' => 'bar'),
|
||||||
|
'Root',
|
||||||
|
null,
|
||||||
|
'foo',
|
||||||
|
null,
|
||||||
|
Form::ERR_INVALID
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertCount(1, $context->getViolations());
|
$this->assertCount(1, $context->getViolations());
|
||||||
$this->assertEquals('Invalid!', $context->getViolations()->get(0)->getMessage());
|
$this->assertEquals($expectedViolation, $context->getViolations()->get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDontValidateConstraintsIfNotSynchronized()
|
public function testDontValidateConstraintsIfNotSynchronized()
|
||||||
@ -517,6 +534,7 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$options = array_replace(array(
|
$options = array_replace(array(
|
||||||
'constraints' => array(),
|
'constraints' => array(),
|
||||||
|
'invalid_message_parameters' => array(),
|
||||||
), $options);
|
), $options);
|
||||||
|
|
||||||
return new FormBuilder($name, $dataClass, $this->dispatcher, $this->factory, $options);
|
return new FormBuilder($name, $dataClass, $this->dispatcher, $this->factory, $options);
|
||||||
|
Reference in New Issue
Block a user