Skip to content

Commit 4620e26

Browse files
committed
[symfony#2963] Disabling validation corectly
1 parent 7c038bf commit 4620e26

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

book/forms.rst

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,10 @@ Disabling Validation
479479
.. versionadded:: 2.3
480480
The ability to set ``validation_groups`` to false was added in Symfony 2.3,
481481
although setting it to an empty array achieved the same result in previous
482-
versions.
482+
versions. Please note that empty array doesn't work anymore.
483483

484484
Sometimes it is useful to suppress the validation of a form altogether. For
485-
these cases, you can skip the call to :method:`Symfony\\Component\\Form\\FormInterface::isValid`
486-
in your controller. If this is not possible, you can alternatively set the
487-
``validation_groups`` option to ``false`` or an empty array::
485+
these cases you can set the ``validation_groups`` option to ``false``::
488486

489487
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
490488

@@ -497,9 +495,8 @@ in your controller. If this is not possible, you can alternatively set the
497495

498496
Note that when you do that, the form will still run basic integrity checks,
499497
for example whether an uploaded file was too large or whether non-existing
500-
fields were submitted. If you want to suppress validation completely, remove
501-
the :method:`Symfony\\Component\\Form\\FormInterface::isValid` call from your
502-
controller.
498+
fields were submitted. If you want to surppress validation you can use
499+
:ref:`POST_SUBMIT event <cookbook-dynamic-form-modification-supressing-form-validation>`
503500

504501
.. index::
505502
single: Forms; Validation groups based on submitted data

cookbook/form/dynamic_form_modification.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,33 @@ after the sport is selected. This should be handled by making an AJAX call
535535
back to your application. In that controller, you can submit your form, but
536536
instead of processing it, simply use the submitted form to render the updated
537537
fields. The response from the AJAX call can then be used to update the view.
538+
539+
.. _cookbook-dynamic-form-modification-supressing-form-validation:
540+
541+
Supressing Form Validation
542+
---------------------------
543+
544+
One way you can use ``POST_SUBMIT`` event is to completely supress
545+
form validation. The reason for that is even if you set ``group_validation``
546+
to ``false`` there still some integrity check are run, for example whether
547+
an uploaded file was too large or whether non-existing fields were submitted.
548+
549+
If you want to suppress even that, you should use ``POST_SUBMIT`` event to prevent
550+
:class:`Symfony\\Component\\Form\\Extension\\Validator\\EventListener\\ValidationListener`
551+
invocation::
552+
553+
use Symfony\Component\Form\FormBuilderInterface;
554+
use Symfony\Component\Form\FormEvents;
555+
556+
public function buildForm(FormBuilderInterface $builder, array $options)
557+
{
558+
$builder->addEventListener(FormEvents::POST_SUBMIT, function($event) {
559+
$event->stopPropagation();
560+
}, /* priority higher than ValidationListener */ 200);
561+
562+
// ...
563+
}
564+
565+
Note that that by doing that you can disable something more than form validation,
566+
because ``POST_SUBMIT`` event can be used for something else.
567+
You also have to know what would be the right priority for disabling POST_SUBMIT events.

0 commit comments

Comments
 (0)