From 6da42ae2d1ae496055cca593b0cdf1d1d3bbb7a8 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 29 Aug 2019 16:32:20 +0200 Subject: [PATCH] dispatch submit events for disabled forms too --- src/Symfony/Component/Form/Form.php | 25 +++++++++++++------ .../Component/Form/Tests/CompoundFormTest.php | 4 +-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index e22415c95e..2631faf618 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -505,10 +505,27 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac // they are collectable during submission only $this->errors = []; + $dispatcher = $this->config->getEventDispatcher(); + // Obviously, a disabled form should not change its data upon submission. - if ($this->isDisabled()) { + if ($this->isDisabled() && $this->isRoot()) { $this->submitted = true; + if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) { + $event = new FormEvent($this, $submittedData); + $dispatcher->dispatch(FormEvents::PRE_SUBMIT, $event); + } + + if ($dispatcher->hasListeners(FormEvents::SUBMIT)) { + $event = new FormEvent($this, $this->getNormData()); + $dispatcher->dispatch(FormEvents::SUBMIT, $event); + } + + if ($dispatcher->hasListeners(FormEvents::POST_SUBMIT)) { + $event = new FormEvent($this, $this->getViewData()); + $dispatcher->dispatch(FormEvents::POST_SUBMIT, $event); + } + return $this; } @@ -538,8 +555,6 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac $this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.'); } - $dispatcher = $this->config->getEventDispatcher(); - $modelData = null; $normData = null; $viewData = null; @@ -752,10 +767,6 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac throw new LogicException('Cannot check if an unsubmitted form is valid. Call Form::isSubmitted() before Form::isValid().'); } - if ($this->isDisabled()) { - return true; - } - return 0 === \count($this->getErrors(true)); } diff --git a/src/Symfony/Component/Form/Tests/CompoundFormTest.php b/src/Symfony/Component/Form/Tests/CompoundFormTest.php index 0a97f6408c..988161953c 100644 --- a/src/Symfony/Component/Form/Tests/CompoundFormTest.php +++ b/src/Symfony/Component/Form/Tests/CompoundFormTest.php @@ -55,7 +55,7 @@ class CompoundFormTest extends AbstractFormTest $this->assertFalse($this->form->isValid()); } - public function testDisabledFormsValidEvenIfChildrenInvalid() + public function testDisabledFormsInvalidEvenChildrenInvalid() { $form = $this->getBuilder('person') ->setDisabled(true) @@ -68,7 +68,7 @@ class CompoundFormTest extends AbstractFormTest $form->get('name')->addError(new FormError('Invalid')); - $this->assertTrue($form->isValid()); + $this->assertFalse($form->isValid()); } public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()