Skip to content

Commit

Permalink
feature #33609 [Form][SubmitType] Add "validate" option (fancyweb)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[Form][SubmitType] Add "validate" option

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #8763
| License       | MIT
| Doc PR        | TODO

The second part of the ticket requires more work but is kind of unrelated.

Commits
-------

a2bc06d [Form][SubmitType] Add "validate" option
  • Loading branch information
xabbuh committed Sep 23, 2019
2 parents 3f96ef2 + a2bc06d commit b00b633
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
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
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
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],
];
}
}
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']);
}
}

0 comments on commit b00b633

Please sign in to comment.