Skip to content

Commit 93b69e3

Browse files
committed
[Form][SubmitType] Add "validate" option
1 parent 21a05de commit 93b69e3

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-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/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)