feature #33381 [Form] dispatch submit events for disabled forms too (xabbuh)

This PR was merged into the 5.2-dev branch.

Discussion
----------

[Form] dispatch submit events for disabled forms too

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27217
| License       | MIT
| Doc PR        |

TODO:

- [ ] add a test case covering the validation use case

Commits
-------

6da42ae2d1 dispatch submit events for disabled forms too
This commit is contained in:
Fabien Potencier 2020-09-01 18:26:17 +02:00
commit dc63d712ab
2 changed files with 20 additions and 9 deletions

View File

@ -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));
}

View File

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