Skip to content

[#2963] Disabling validation corectly #3346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 <cookbook-dynamic-form-modification-suppressing-form-validation>`

.. index::
single: Forms; Validation groups based on submitted data
Expand Down
30 changes: 30 additions & 0 deletions cookbook/form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have one dash to much here


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.