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:
commit
dc63d712ab
@ -505,10 +505,27 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac
|
|||||||
// they are collectable during submission only
|
// they are collectable during submission only
|
||||||
$this->errors = [];
|
$this->errors = [];
|
||||||
|
|
||||||
|
$dispatcher = $this->config->getEventDispatcher();
|
||||||
|
|
||||||
// Obviously, a disabled form should not change its data upon submission.
|
// Obviously, a disabled form should not change its data upon submission.
|
||||||
if ($this->isDisabled()) {
|
if ($this->isDisabled() && $this->isRoot()) {
|
||||||
$this->submitted = true;
|
$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;
|
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.');
|
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$dispatcher = $this->config->getEventDispatcher();
|
|
||||||
|
|
||||||
$modelData = null;
|
$modelData = null;
|
||||||
$normData = null;
|
$normData = null;
|
||||||
$viewData = 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().');
|
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));
|
return 0 === \count($this->getErrors(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ class CompoundFormTest extends AbstractFormTest
|
|||||||
$this->assertFalse($this->form->isValid());
|
$this->assertFalse($this->form->isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDisabledFormsValidEvenIfChildrenInvalid()
|
public function testDisabledFormsInvalidEvenChildrenInvalid()
|
||||||
{
|
{
|
||||||
$form = $this->getBuilder('person')
|
$form = $this->getBuilder('person')
|
||||||
->setDisabled(true)
|
->setDisabled(true)
|
||||||
@ -68,7 +68,7 @@ class CompoundFormTest extends AbstractFormTest
|
|||||||
|
|
||||||
$form->get('name')->addError(new FormError('Invalid'));
|
$form->get('name')->addError(new FormError('Invalid'));
|
||||||
|
|
||||||
$this->assertTrue($form->isValid());
|
$this->assertFalse($form->isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()
|
public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()
|
||||||
|
Reference in New Issue
Block a user