Skip to content

[Form] dispatch submit events for disabled forms too #33381

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 1, 2020
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
25 changes: 18 additions & 7 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,10 +505,27 @@ public function submit($submittedData, bool $clearMissing = true)
// 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()) {
Copy link
Member

@yceruto yceruto Sep 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for late feedback, but, if I'm not missing something, I think this change will break the current behavior.

Assuming you have a child field disabled, getting its value this way $form->get('field')->getData() will return the submitted data rather than its initial value as expected.

It is likely that the new condition $this->isRoot() should group the new code (the events being dispatched) in place, thus keeping the previous behavior of ignoring disabled child forms.

$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;
}

Expand Down Expand Up @@ -538,8 +555,6 @@ public function submit($submittedData, bool $clearMissing = true)
$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;
Expand Down Expand Up @@ -752,10 +767,6 @@ public function isValid()
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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testInvalidIfChildIsInvalid()
$this->assertFalse($this->form->isValid());
}

public function testDisabledFormsValidEvenIfChildrenInvalid()
public function testDisabledFormsInvalidEvenChildrenInvalid()
{
$form = $this->getBuilder('person')
->setDisabled(true)
Expand All @@ -68,7 +68,7 @@ public function testDisabledFormsValidEvenIfChildrenInvalid()

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

$this->assertTrue($form->isValid());
$this->assertFalse($form->isValid());
}

public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()
Expand Down