Skip to content

Commit d6edcdf

Browse files
committed
Merge pull request symfony#3346 from piorek/ticket_2963
[symfony#2963] Disabling validation corectly
2 parents 05791bb + 86802e2 commit d6edcdf

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

book/forms.rst

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,10 @@ Disabling Validation
477477
~~~~~~~~~~~~~~~~~~~~
478478

479479
.. versionadded:: 2.3
480-
The ability to set ``validation_groups`` to false was added in Symfony 2.3,
481-
although setting it to an empty array achieved the same result in previous
482-
versions.
480+
The ability to set ``validation_groups`` to false was added in Symfony 2.3.
483481

484482
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::
483+
these cases you can set the ``validation_groups`` option to ``false``::
488484

489485
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
490486

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

498494
Note that when you do that, the form will still run basic integrity checks,
499495
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.
496+
fields were submitted. If you want to suppress validation, you can use the
497+
:ref:`POST_SUBMIT event <cookbook-dynamic-form-modification-suppressing-form-validation>`
503498

504499
.. index::
505500
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
@@ -592,3 +592,33 @@ after the sport is selected. This should be handled by making an AJAX call
592592
back to your application. In that controller, you can submit your form, but
593593
instead of processing it, simply use the submitted form to render the updated
594594
fields. The response from the AJAX call can then be used to update the view.
595+
596+
.. _cookbook-dynamic-form-modification-suppressing-form-validation:
597+
598+
Suppressing Form Validation
599+
---------------------------
600+
601+
To suppress form validation you can use the ``POST_SUBMIT`` event and prevent
602+
:class:`Symfony\\Component\\Form\\Extension\\Validator\\EventListener\\ValidationListener`
603+
invocation.
604+
605+
The reason for this is even if you set ``group_validation`` to ``false`` there
606+
are still some integrity checks executed, for example whether an uploaded file
607+
was too large or whether non-existing fields were submitted::
608+
609+
use Symfony\Component\Form\FormBuilderInterface;
610+
use Symfony\Component\Form\FormEvents;
611+
612+
public function buildForm(FormBuilderInterface $builder, array $options)
613+
{
614+
$builder->addEventListener(FormEvents::POST_SUBMIT, function($event) {
615+
$event->stopPropagation();
616+
}, 900); // Always set a higher priority than ValidationListener
617+
618+
// ...
619+
}
620+
621+
.. caution::
622+
623+
By doing this, you can disable something more than just form validation,
624+
because the ``POST_SUBMIT`` event can be used for something else too.

0 commit comments

Comments
 (0)