Skip to content

[Form][SubmitType] Add "validate" option #33609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/SubmitType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}