Skip to content

Commit 6da42ae

Browse files
committed
dispatch submit events for disabled forms too
1 parent ccfc4ba commit 6da42ae

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,27 @@ public function submit($submittedData, bool $clearMissing = true)
505505
// they are collectable during submission only
506506
$this->errors = [];
507507

508+
$dispatcher = $this->config->getEventDispatcher();
509+
508510
// Obviously, a disabled form should not change its data upon submission.
509-
if ($this->isDisabled()) {
511+
if ($this->isDisabled() && $this->isRoot()) {
510512
$this->submitted = true;
511513

514+
if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) {
515+
$event = new FormEvent($this, $submittedData);
516+
$dispatcher->dispatch(FormEvents::PRE_SUBMIT, $event);
517+
}
518+
519+
if ($dispatcher->hasListeners(FormEvents::SUBMIT)) {
520+
$event = new FormEvent($this, $this->getNormData());
521+
$dispatcher->dispatch(FormEvents::SUBMIT, $event);
522+
}
523+
524+
if ($dispatcher->hasListeners(FormEvents::POST_SUBMIT)) {
525+
$event = new FormEvent($this, $this->getViewData());
526+
$dispatcher->dispatch(FormEvents::POST_SUBMIT, $event);
527+
}
528+
512529
return $this;
513530
}
514531

@@ -538,8 +555,6 @@ public function submit($submittedData, bool $clearMissing = true)
538555
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');
539556
}
540557

541-
$dispatcher = $this->config->getEventDispatcher();
542-
543558
$modelData = null;
544559
$normData = null;
545560
$viewData = null;
@@ -752,10 +767,6 @@ public function isValid()
752767
throw new LogicException('Cannot check if an unsubmitted form is valid. Call Form::isSubmitted() before Form::isValid().');
753768
}
754769

755-
if ($this->isDisabled()) {
756-
return true;
757-
}
758-
759770
return 0 === \count($this->getErrors(true));
760771
}
761772

src/Symfony/Component/Form/Tests/CompoundFormTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testInvalidIfChildIsInvalid()
5555
$this->assertFalse($this->form->isValid());
5656
}
5757

58-
public function testDisabledFormsValidEvenIfChildrenInvalid()
58+
public function testDisabledFormsInvalidEvenChildrenInvalid()
5959
{
6060
$form = $this->getBuilder('person')
6161
->setDisabled(true)
@@ -68,7 +68,7 @@ public function testDisabledFormsValidEvenIfChildrenInvalid()
6868

6969
$form->get('name')->addError(new FormError('Invalid'));
7070

71-
$this->assertTrue($form->isValid());
71+
$this->assertFalse($form->isValid());
7272
}
7373

7474
public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()

0 commit comments

Comments
 (0)