Skip to content

Commit d5ecb49

Browse files
committed
[Form][SubmitType] Add "validate" option
1 parent d4e6a37 commit d5ecb49

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* The type guesser guesses the HTML accept attribute when a mime type is configured in the File or Image constraint.
1212
* Overriding the methods `FormIntegrationTestCase::setUp()`, `TypeTestCase::setUp()` and `TypeTestCase::tearDown()` without the `void` return-type is deprecated.
1313
* marked all dispatched event classes as `@final`
14+
* Added the `validate` option to `SubmitType` to toggle the browser built-in form validation.
1415

1516
4.3.0
1617
-----

src/Symfony/Component/Form/Extension/Core/Type/SubmitType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\FormInterface;
1616
use Symfony\Component\Form\FormView;
1717
use Symfony\Component\Form\SubmitButtonTypeInterface;
18+
use Symfony\Component\OptionsResolver\OptionsResolver;
1819

1920
/**
2021
* A submit button.
@@ -26,6 +27,19 @@ class SubmitType extends AbstractType implements SubmitButtonTypeInterface
2627
public function buildView(FormView $view, FormInterface $form, array $options)
2728
{
2829
$view->vars['clicked'] = $form->isClicked();
30+
31+
if (!$options['validate']) {
32+
$view->vars['attr']['formnovalidate'] = true;
33+
}
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function configureOptions(OptionsResolver $resolver)
40+
{
41+
$resolver->setDefault('validate', true);
42+
$resolver->setAllowedTypes('validate', 'bool');
2943
}
3044

3145
/**

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\SkippedTestError;
1515
use Symfony\Component\Form\Extension\Core\Type\PercentType;
16+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
1617
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
1718
use Symfony\Component\Form\FormError;
1819
use Symfony\Component\Form\FormView;
@@ -2730,4 +2731,38 @@ public function testButtonWithTranslationParameters()
27302731
'
27312732
);
27322733
}
2734+
2735+
/**
2736+
* @dataProvider submitFormNoValidateProvider
2737+
*/
2738+
public function testSubmitFormNoValidate(bool $attributeIsExpected, bool $validate)
2739+
{
2740+
$this->requiresFeatureSet(404);
2741+
2742+
$form = $this->factory->create(SubmitType::class, null, [
2743+
'validate' => $validate,
2744+
]);
2745+
2746+
$html = $this->renderWidget($form->createView());
2747+
2748+
$xpath = '/button
2749+
[@type="submit"]
2750+
';
2751+
2752+
if ($attributeIsExpected) {
2753+
$xpath .= '[@formnovalidate="formnovalidate"]';
2754+
} else {
2755+
$xpath .= '[not(@formnovalidate="formnovalidate")]';
2756+
}
2757+
2758+
$this->assertMatchesXpath($html, $xpath);
2759+
}
2760+
2761+
public function submitFormNoValidateProvider()
2762+
{
2763+
return [
2764+
[true, false],
2765+
[false, true],
2766+
];
2767+
}
27332768
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/SubmitTypeTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ public function testSubmitCanBeAddedToForm()
6262

6363
$this->assertSame($form, $form->add('send', static::TESTED_TYPE));
6464
}
65+
66+
public function testFormNoValidate()
67+
{
68+
$this->assertTrue($this->factory->create(static::TESTED_TYPE, null, [
69+
'validate' => false,
70+
])->createView()->vars['attr']['formnovalidate']);
71+
}
6572
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy