[Form] The option "validation_groups" can now be set to false to disable validation. This is identical to setting it to an empty array.
This commit is contained in:
parent
277d6dfcf7
commit
600007b71f
@ -28,6 +28,8 @@ CHANGELOG
|
|||||||
* added an optional PropertyAccessorInterface parameter to FormType,
|
* added an optional PropertyAccessorInterface parameter to FormType,
|
||||||
ObjectChoiceList and PropertyPathMapper
|
ObjectChoiceList and PropertyPathMapper
|
||||||
* [BC BREAK] PropertyPathMapper and FormType now have a constructor
|
* [BC BREAK] PropertyPathMapper and FormType now have a constructor
|
||||||
|
* [BC BREAK] setting the option "validation_groups" to ``false`` now disables validation
|
||||||
|
instead of assuming group "Default"
|
||||||
|
|
||||||
2.1.0
|
2.1.0
|
||||||
-----
|
-----
|
||||||
|
@ -30,6 +30,10 @@ abstract class BaseValidatorExtension extends AbstractTypeExtension
|
|||||||
{
|
{
|
||||||
// Make sure that validation groups end up as null, closure or array
|
// Make sure that validation groups end up as null, closure or array
|
||||||
$validationGroupsNormalizer = function (Options $options, $groups) {
|
$validationGroupsNormalizer = function (Options $options, $groups) {
|
||||||
|
if (false === $groups) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($groups)) {
|
if (empty($groups)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -170,6 +170,51 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->validator->validate($form, new Form());
|
$this->validator->validate($form, new Form());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDontValidateIfNoValidationGroups()
|
||||||
|
{
|
||||||
|
$context = $this->getMockExecutionContext();
|
||||||
|
$object = $this->getMock('\stdClass');
|
||||||
|
|
||||||
|
$form = $this->getBuilder('name', '\stdClass', array(
|
||||||
|
'validation_groups' => array(),
|
||||||
|
))
|
||||||
|
->setData($object)
|
||||||
|
->getForm();
|
||||||
|
|
||||||
|
$form->setData($object);
|
||||||
|
|
||||||
|
$context->expects($this->never())
|
||||||
|
->method('validate');
|
||||||
|
|
||||||
|
$this->validator->initialize($context);
|
||||||
|
$this->validator->validate($form, new Form());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDontValidateConstraintsIfNoValidationGroups()
|
||||||
|
{
|
||||||
|
$context = $this->getMockExecutionContext();
|
||||||
|
$object = $this->getMock('\stdClass');
|
||||||
|
$constraint1 = $this->getMock('Symfony\Component\Validator\Constraint');
|
||||||
|
$constraint2 = $this->getMock('Symfony\Component\Validator\Constraint');
|
||||||
|
|
||||||
|
$options = array(
|
||||||
|
'validation_groups' => array(),
|
||||||
|
'constraints' => array($constraint1, $constraint2),
|
||||||
|
);
|
||||||
|
$form = $this->getBuilder('name', '\stdClass', $options)
|
||||||
|
->setData($object)
|
||||||
|
->getForm();
|
||||||
|
|
||||||
|
// Launch transformer
|
||||||
|
$form->bind(array());
|
||||||
|
|
||||||
|
$context->expects($this->never())
|
||||||
|
->method('validate');
|
||||||
|
|
||||||
|
$this->validator->initialize($context);
|
||||||
|
$this->validator->validate($form, new Form());
|
||||||
|
}
|
||||||
|
|
||||||
public function testDontValidateIfNotSynchronized()
|
public function testDontValidateIfNotSynchronized()
|
||||||
{
|
{
|
||||||
$context = $this->getMockExecutionContext();
|
$context = $this->getMockExecutionContext();
|
||||||
@ -209,6 +254,46 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->validator->validate($form, new Form());
|
$this->validator->validate($form, new Form());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAddInvalidErrorEvenIfNoValidationGroups()
|
||||||
|
{
|
||||||
|
$context = $this->getMockExecutionContext();
|
||||||
|
$object = $this->getMock('\stdClass');
|
||||||
|
|
||||||
|
$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'),
|
||||||
|
'validation_groups' => array(),
|
||||||
|
))
|
||||||
|
->setData($object)
|
||||||
|
->addViewTransformer(new CallbackTransformer(
|
||||||
|
function ($data) { return $data; },
|
||||||
|
function () { throw new TransformationFailedException(); }
|
||||||
|
))
|
||||||
|
->getForm();
|
||||||
|
|
||||||
|
// Launch transformer
|
||||||
|
$form->bind('foo');
|
||||||
|
|
||||||
|
$context->expects($this->never())
|
||||||
|
->method('validate');
|
||||||
|
|
||||||
|
$context->expects($this->once())
|
||||||
|
->method('addViolation')
|
||||||
|
->with(
|
||||||
|
'invalid_message_key',
|
||||||
|
array('{{ value }}' => 'foo', '{{ foo }}' => 'bar'),
|
||||||
|
'foo'
|
||||||
|
);
|
||||||
|
$context->expects($this->never())
|
||||||
|
->method('addViolationAt');
|
||||||
|
|
||||||
|
$this->validator->initialize($context);
|
||||||
|
$this->validator->validate($form, new Form());
|
||||||
|
}
|
||||||
|
|
||||||
public function testDontValidateConstraintsIfNotSynchronized()
|
public function testDontValidateConstraintsIfNotSynchronized()
|
||||||
{
|
{
|
||||||
$context = $this->getMockExecutionContext();
|
$context = $this->getMockExecutionContext();
|
||||||
|
@ -40,6 +40,15 @@ class FormTypeValidatorExtensionTest extends TypeTestCase
|
|||||||
$this->assertEquals(array('group1', 'group2'), $form->getConfig()->getOption('validation_groups'));
|
$this->assertEquals(array('group1', 'group2'), $form->getConfig()->getOption('validation_groups'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testValidationGroupsCanBeSetToFalse()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create('form', null, array(
|
||||||
|
'validation_groups' => false,
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEquals(array(), $form->getConfig()->getOption('validation_groups'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testValidationGroupsCanBeSetToCallback()
|
public function testValidationGroupsCanBeSetToCallback()
|
||||||
{
|
{
|
||||||
$form = $this->factory->create('form', null, array(
|
$form = $this->factory->create('form', null, array(
|
||||||
|
Reference in New Issue
Block a user