Skip to content

Commit 09fb97a

Browse files
committed
[Form] The option "validation_groups" can now be set to false to disable validation. This is identical to setting it to an empty array.
1 parent a7d9204 commit 09fb97a

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ CHANGELOG
2828
* added an optional PropertyAccessorInterface parameter to FormType,
2929
ObjectChoiceList and PropertyPathMapper
3030
* [BC BREAK] PropertyPathMapper and FormType now have a constructor
31+
* [BC BREAK] setting the option "validation_groups" to ``false`` now disables validation
32+
instead of assuming group "Default"
3133

3234
2.1.0
3335
-----

Extension/Validator/Type/BaseValidatorExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
3030
{
3131
// Make sure that validation groups end up as null, closure or array
3232
$validationGroupsNormalizer = function (Options $options, $groups) {
33+
if (false === $groups) {
34+
return array();
35+
}
36+
3337
if (empty($groups)) {
3438
return null;
3539
}

Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,51 @@ public function testValidateConstraintsEvenIfNoCascadeValidation()
170170
$this->validator->validate($form, new Form());
171171
}
172172

173+
public function testDontValidateIfNoValidationGroups()
174+
{
175+
$context = $this->getMockExecutionContext();
176+
$object = $this->getMock('\stdClass');
177+
178+
$form = $this->getBuilder('name', '\stdClass', array(
179+
'validation_groups' => array(),
180+
))
181+
->setData($object)
182+
->getForm();
183+
184+
$form->setData($object);
185+
186+
$context->expects($this->never())
187+
->method('validate');
188+
189+
$this->validator->initialize($context);
190+
$this->validator->validate($form, new Form());
191+
}
192+
193+
public function testDontValidateConstraintsIfNoValidationGroups()
194+
{
195+
$context = $this->getMockExecutionContext();
196+
$object = $this->getMock('\stdClass');
197+
$constraint1 = $this->getMock('Symfony\Component\Validator\Constraint');
198+
$constraint2 = $this->getMock('Symfony\Component\Validator\Constraint');
199+
200+
$options = array(
201+
'validation_groups' => array(),
202+
'constraints' => array($constraint1, $constraint2),
203+
);
204+
$form = $this->getBuilder('name', '\stdClass', $options)
205+
->setData($object)
206+
->getForm();
207+
208+
// Launch transformer
209+
$form->bind(array());
210+
211+
$context->expects($this->never())
212+
->method('validate');
213+
214+
$this->validator->initialize($context);
215+
$this->validator->validate($form, new Form());
216+
}
217+
173218
public function testDontValidateIfNotSynchronized()
174219
{
175220
$context = $this->getMockExecutionContext();
@@ -209,6 +254,46 @@ function () { throw new TransformationFailedException(); }
209254
$this->validator->validate($form, new Form());
210255
}
211256

257+
public function testAddInvalidErrorEvenIfNoValidationGroups()
258+
{
259+
$context = $this->getMockExecutionContext();
260+
$object = $this->getMock('\stdClass');
261+
262+
$form = $this->getBuilder('name', '\stdClass', array(
263+
'invalid_message' => 'invalid_message_key',
264+
// Invalid message parameters must be supported, because the
265+
// invalid message can be a translation key
266+
// see https://github.com/symfony/symfony/issues/5144
267+
'invalid_message_parameters' => array('{{ foo }}' => 'bar'),
268+
'validation_groups' => array(),
269+
))
270+
->setData($object)
271+
->addViewTransformer(new CallbackTransformer(
272+
function ($data) { return $data; },
273+
function () { throw new TransformationFailedException(); }
274+
))
275+
->getForm();
276+
277+
// Launch transformer
278+
$form->bind('foo');
279+
280+
$context->expects($this->never())
281+
->method('validate');
282+
283+
$context->expects($this->once())
284+
->method('addViolation')
285+
->with(
286+
'invalid_message_key',
287+
array('{{ value }}' => 'foo', '{{ foo }}' => 'bar'),
288+
'foo'
289+
);
290+
$context->expects($this->never())
291+
->method('addViolationAt');
292+
293+
$this->validator->initialize($context);
294+
$this->validator->validate($form, new Form());
295+
}
296+
212297
public function testDontValidateConstraintsIfNotSynchronized()
213298
{
214299
$context = $this->getMockExecutionContext();

Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function testValidationGroupsCanBeSetToArray()
4040
$this->assertEquals(array('group1', 'group2'), $form->getConfig()->getOption('validation_groups'));
4141
}
4242

43+
public function testValidationGroupsCanBeSetToFalse()
44+
{
45+
$form = $this->factory->create('form', null, array(
46+
'validation_groups' => false,
47+
));
48+
49+
$this->assertEquals(array(), $form->getConfig()->getOption('validation_groups'));
50+
}
51+
4352
public function testValidationGroupsCanBeSetToCallback()
4453
{
4554
$form = $this->factory->create('form', null, array(

0 commit comments

Comments
 (0)