[Form][SubmitType] Add "validate" option

This commit is contained in:
Thomas Calvet 2019-09-17 11:10:23 +02:00
parent d4e6a37b14
commit a2bc06d811
4 changed files with 57 additions and 0 deletions

View File

@ -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
-----

View File

@ -15,6 +15,7 @@ use Symfony\Component\Form\AbstractType;
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');
}
/**

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Tests;
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 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
'
);
}
/**
* @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],
];
}
}

View File

@ -62,4 +62,11 @@ class SubmitTypeTest extends ButtonTypeTest
$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']);
}
}