Skip to content

Instantly share code, notes, and snippets.

@MaximePinot
Last active July 21, 2022 09:49
Show Gist options
  • Save MaximePinot/d8c8204d059acf1bf880be5156310a18 to your computer and use it in GitHub Desktop.
Save MaximePinot/d8c8204d059acf1bf880be5156310a18 to your computer and use it in GitHub Desktop.
Disable client-side validation on all forms. This is useful in dev and test environments.
CLIENT_SIDE_VALIDATION_ENABLED=1
CLIENT_SIDE_VALIDATION_ENABLED=0
CLIENT_SIDE_VALIDATION_ENABLED=0
<?php
declare(strict_types=1);
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
final class NoValidateExtension extends AbstractTypeExtension
{
private bool $clientSideValidationEnabled;
public function __construct(bool $clientSideValidationEnabled)
{
$this->clientSideValidationEnabled = $clientSideValidationEnabled;
}
/**
* @return iterable<string>
*/
public static function getExtendedTypes(): iterable
{
return [FormType::class];
}
/**
* {@inheritdoc}
*
* @param FormInterface<Form> $form
* @param array<string, mixed> $options
*/
public function buildView(FormView $view, FormInterface $form, array $options): void
{
if (null === $view->parent && !$this->clientSideValidationEnabled) {
$view->vars['attr']['novalidate'] = 'novalidate';
}
}
}
services:
# ...
App\Form\Extension\NoValidateExtension:
arguments: ['%env(bool:CLIENT_SIDE_VALIDATION_ENABLED)%']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment