diff --git a/book/forms.rst b/book/forms.rst index 339e886a6d3..cf45ee81d03 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -477,14 +477,10 @@ Disabling Validation ~~~~~~~~~~~~~~~~~~~~ .. versionadded:: 2.3 - The ability to set ``validation_groups`` to false was added in Symfony 2.3, - although setting it to an empty array achieved the same result in previous - versions. + The ability to set ``validation_groups`` to false was added in Symfony 2.3. Sometimes it is useful to suppress the validation of a form altogether. For -these cases, you can skip the call to :method:`Symfony\\Component\\Form\\FormInterface::isValid` -in your controller. If this is not possible, you can alternatively set the -``validation_groups`` option to ``false`` or an empty array:: +these cases you can set the ``validation_groups`` option to ``false``:: use Symfony\Component\OptionsResolver\OptionsResolverInterface; @@ -497,9 +493,8 @@ in your controller. If this is not possible, you can alternatively set the Note that when you do that, the form will still run basic integrity checks, for example whether an uploaded file was too large or whether non-existing -fields were submitted. If you want to suppress validation completely, remove -the :method:`Symfony\\Component\\Form\\FormInterface::isValid` call from your -controller. +fields were submitted. If you want to suppress validation, you can use the +:ref:`POST_SUBMIT event ` .. index:: single: Forms; Validation groups based on submitted data diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index 68c5e98d663..b3046390e6a 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -535,3 +535,33 @@ after the sport is selected. This should be handled by making an AJAX call back to your application. In that controller, you can submit your form, but instead of processing it, simply use the submitted form to render the updated fields. The response from the AJAX call can then be used to update the view. + +.. _cookbook-dynamic-form-modification-suppressing-form-validation: + +Suppressing Form Validation +--------------------------- + +To suppress form validation you can use the ``POST_SUBMIT`` event and prevent +:class:`Symfony\\Component\\Form\\Extension\\Validator\\EventListener\\ValidationListener` +invocation. + +The reason for this is even if you set ``group_validation`` to ``false`` there +are still some integrity checks executed, for example whether an uploaded file +was too large or whether non-existing fields were submitted:: + + use Symfony\Component\Form\FormBuilderInterface; + use Symfony\Component\Form\FormEvents; + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->addEventListener(FormEvents::POST_SUBMIT, function($event) { + $event->stopPropagation(); + }, 900); // Always set a higher priority than ValidationListener + + // ... + } + +.. caution:: + + By doing this, you can disable something more than just form validation, + because the ``POST_SUBMIT`` event can be used for something else too.