diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 7e94a8dd95c24..e91444bcff61b 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -11,6 +11,7 @@ CHANGELOG * The type guesser guesses the HTML accept attribute when a mime type is configured in the File or Image constraint. * Overriding the methods `FormIntegrationTestCase::setUp()`, `TypeTestCase::setUp()` and `TypeTestCase::tearDown()` without the `void` return-type is deprecated. * marked all dispatched event classes as `@final` + * Added the `validate` option to `SubmitType` to toggle the browser built-in form validation. 4.3.0 ----- diff --git a/src/Symfony/Component/Form/Extension/Core/Type/SubmitType.php b/src/Symfony/Component/Form/Extension/Core/Type/SubmitType.php index 38666325f374a..bf3cea2414484 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/SubmitType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/SubmitType.php @@ -15,6 +15,7 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\Form\SubmitButtonTypeInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; /** * A submit button. @@ -26,6 +27,19 @@ class SubmitType extends AbstractType implements SubmitButtonTypeInterface public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['clicked'] = $form->isClicked(); + + if (!$options['validate']) { + $view->vars['attr']['formnovalidate'] = true; + } + } + + /** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefault('validate', true); + $resolver->setAllowedTypes('validate', 'bool'); } /** diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index bea20dc8f84ab..8f1231e3bc293 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\SkippedTestError; use Symfony\Component\Form\Extension\Core\Type\PercentType; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Csrf\CsrfExtension; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormView; @@ -2730,4 +2731,38 @@ public function testButtonWithTranslationParameters() ' ); } + + /** + * @dataProvider submitFormNoValidateProvider + */ + public function testSubmitFormNoValidate(bool $validate) + { + $this->requiresFeatureSet(404); + + $form = $this->factory->create(SubmitType::class, null, [ + 'validate' => $validate, + ]); + + $html = $this->renderWidget($form->createView()); + + $xpath = '/button + [@type="submit"] + '; + + if (!$validate) { + $xpath .= '[@formnovalidate="formnovalidate"]'; + } else { + $xpath .= '[not(@formnovalidate="formnovalidate")]'; + } + + $this->assertMatchesXpath($html, $xpath); + } + + public function submitFormNoValidateProvider() + { + return [ + [false], + [true], + ]; + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/SubmitTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/SubmitTypeTest.php index e86704bcfb2ad..ea9c9d38dc3eb 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/SubmitTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/SubmitTypeTest.php @@ -62,4 +62,11 @@ public function testSubmitCanBeAddedToForm() $this->assertSame($form, $form->add('send', static::TESTED_TYPE)); } + + public function testFormNoValidate() + { + $this->assertTrue($this->factory->create(static::TESTED_TYPE, null, [ + 'validate' => false, + ])->createView()->vars['attr']['formnovalidate']); + } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: