Symfony2 disable HTML5 form validation Symfony2 disable HTML5 form validation symfony symfony

Symfony2 disable HTML5 form validation


Just add novalidate to your <form> tag:

<form novalidate>

If you are rendering the form in TWIG, you can use following.

{{ form(form, {'attr': {'novalidate': 'novalidate'}}) }}


I know its old question but with SF2.6 in FormType, you can do:

/** * @param OptionsResolverInterface $resolver */public function setDefaultOptions(OptionsResolverInterface $resolver){    $resolver->setDefaults(array(        'attr'=>array('novalidate'=>'novalidate')    ));}


While googling for a solution to this I found one, that seems the most elegant if you want to disable html5 validation in your whole app, so I thought i'd share it here. Credits go to the author of this blog article.

The idea is to create an extension for the "form" form type like this:

<?php// src/AppBundle/Form/Extension/NoValidateExtension.phpnamespace AppBundle\Form\Extension;use Symfony\Component\Form\AbstractTypeExtension;use Symfony\Component\Form\FormInterface;use Symfony\Component\Form\FormView;class NoValidateExtension extends AbstractTypeExtension{    public function buildView(FormView $view, FormInterface $form, array $options)    {        $view->vars['attr'] = array_merge($view->vars['attr'], [            'novalidate' => 'novalidate',        ]);    }    public function getExtendedType()    {        return 'form';    }}?>

Then you just register it in your services.yml like this:

app.no_validation_form_extension:    class: AppBundle\Form\Extension\NoValidateExtension    tags:        - {name: form.type_extension, alias: form}

and you're done. All your forms automatically have a novalidate attribute now.

Symfony 3.3

As of Symfony 3.3 the configuration is slightly different, but still possible.

Slight update to the getExtendedType method to return the FormType class.

// src/AppBundle/Form/Extension/NoValidateExtension.phpnamespace AppBundle\Form\Extension;use Symfony\Component\Form\AbstractTypeExtension;use Symfony\Component\Form\FormInterface;use Symfony\Component\Form\FormView;use Symfony\Component\Form\Extension\Core\Type\FormType;class NoValidateExtension extends AbstractTypeExtension{    public function buildView(FormView $view, FormInterface $form, array $options)    {        $view->vars['attr'] = array_merge($view->vars['attr'], [            'novalidate' => 'novalidate',        ]);    }    public function getExtendedType()    {        return FormType::class;    }}

Plus some a minor addition of the extended_type tag, which is now required in your service declaration:

app.no_validation_form_extension:    class: AppBundle\Form\Extension\NoValidateExtension    tags:        - {name: form.type_extension, alias: form, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType}