-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLIENT_SIDE_VALIDATION_ENABLED=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLIENT_SIDE_VALIDATION_ENABLED=0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLIENT_SIDE_VALIDATION_ENABLED=0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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