From 4620e26a98a0407ccef1217f74f367f8abbaec83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pior?= Date: Sun, 15 Dec 2013 16:41:44 +0100 Subject: [PATCH 1/6] [#2963] Disabling validation corectly --- book/forms.rst | 11 +++----- cookbook/form/dynamic_form_modification.rst | 30 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index 339e886a6d3..ef3eaa1cdf4 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -479,12 +479,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. + versions. Please note that empty array doesn't work anymore. 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 +495,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 surppress validation you can use +: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..f9b6824ef7a 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-supressing-form-validation: + +Supressing Form Validation +--------------------------- + +One way you can use ``POST_SUBMIT`` event is to completely supress +form validation. The reason for that is even if you set ``group_validation`` +to ``false`` there still some integrity check are run, for example whether +an uploaded file was too large or whether non-existing fields were submitted. + +If you want to suppress even that, you should use ``POST_SUBMIT`` event to prevent +:class:`Symfony\\Component\\Form\\Extension\\Validator\\EventListener\\ValidationListener` +invocation:: + + 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(); + }, /* priority higher than ValidationListener */ 200); + + // ... + } + +Note that that by doing that you can disable something more than form validation, +because ``POST_SUBMIT`` event can be used for something else. +You also have to know what would be the right priority for disabling POST_SUBMIT events. From 68d0ee24e8af502ff8042ef29707db463a057f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pior?= Date: Sun, 15 Dec 2013 21:10:11 +0100 Subject: [PATCH 2/6] Corrected docs according to WouterJ comments --- book/forms.rst | 4 ++-- cookbook/form/dynamic_form_modification.rst | 26 ++++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index ef3eaa1cdf4..87372d0f637 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -495,8 +495,8 @@ these cases you can set the ``validation_groups`` option to ``false``:: 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 surppress validation you can use -:ref:`POST_SUBMIT event ` +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 f9b6824ef7a..ccaa992e8a4 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -536,19 +536,18 @@ 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-supressing-form-validation: +.. _cookbook-dynamic-form-modification-suppressing-form-validation: -Supressing Form Validation +Suppressing Form Validation --------------------------- -One way you can use ``POST_SUBMIT`` event is to completely supress -form validation. The reason for that is even if you set ``group_validation`` -to ``false`` there still some integrity check are run, for example whether -an uploaded file was too large or whether non-existing fields were submitted. +To suppress form validation you can use the ``POST_SUBMIT`` event and prevent +:class:`Symfony\\Component\\Form\\Extension\\Validator\\EventListener\\ValidationListener` +invocation. -If you want to suppress even that, you should use ``POST_SUBMIT`` event to 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; @@ -557,11 +556,12 @@ invocation:: { $builder->addEventListener(FormEvents::POST_SUBMIT, function($event) { $event->stopPropagation(); - }, /* priority higher than ValidationListener */ 200); + }, /* priority higher than ValidationListener */ 900); // ... } -Note that that by doing that you can disable something more than form validation, -because ``POST_SUBMIT`` event can be used for something else. -You also have to know what would be the right priority for disabling POST_SUBMIT events. +.. caution:: + + By doing this, you can disable something more than just form validation, + because ``POST_SUBMIT`` event can be used for something else. From 3909762c98ea86e44d149023ad401cbbb2862dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pior?= Date: Sun, 15 Dec 2013 21:28:26 +0100 Subject: [PATCH 3/6] One more correction --- book/forms.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index 87372d0f637..cf45ee81d03 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -477,9 +477,7 @@ 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. Please note that empty array doesn't work anymore. + 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 set the ``validation_groups`` option to ``false``:: From b5f6a49dcc2d971ce24ffc5828b8361d4c92b2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pior?= Date: Sun, 15 Dec 2013 21:40:58 +0100 Subject: [PATCH 4/6] One more correction --- cookbook/form/dynamic_form_modification.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index ccaa992e8a4..bb462d79d50 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -564,4 +564,4 @@ was too large or whether non-existing fields were submitted:: .. caution:: By doing this, you can disable something more than just form validation, - because ``POST_SUBMIT`` event can be used for something else. + because the ``POST_SUBMIT`` event can be used for something else too. From 9d14517fd68a313be7783f037261d8af0374a6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pior?= Date: Sun, 15 Dec 2013 22:21:40 +0100 Subject: [PATCH 5/6] One more correction --- cookbook/form/dynamic_form_modification.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index bb462d79d50..c3e74dbaeee 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -556,7 +556,7 @@ was too large or whether non-existing fields were submitted:: { $builder->addEventListener(FormEvents::POST_SUBMIT, function($event) { $event->stopPropagation(); - }, /* priority higher than ValidationListener */ 900); + }, 900); /* priority higher than ValidationListener */ // ... } From 86802e2210af09016edc9733c666c17cef0854ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pior?= Date: Sun, 15 Dec 2013 23:07:55 +0100 Subject: [PATCH 6/6] Another correction --- cookbook/form/dynamic_form_modification.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index c3e74dbaeee..b3046390e6a 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -556,7 +556,7 @@ was too large or whether non-existing fields were submitted:: { $builder->addEventListener(FormEvents::POST_SUBMIT, function($event) { $event->stopPropagation(); - }, 900); /* priority higher than ValidationListener */ + }, 900); // Always set a higher priority than ValidationListener // ... }