Skip to content

Commit

Permalink
bug #2553 [LiveComponent] Fix ComponentWithFormTrait not working in b…
Browse files Browse the repository at this point in the history
…atch actions (smnandre)

This PR was merged into the 2.x branch.

Discussion
----------

[LiveComponent] Fix ComponentWithFormTrait not working in batch actions

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        | Fix #1509
| License       | MIT

Fix #1509

Thank you `@jpvdw86` for your work on this! 👏

Some parts of your PR would have introduced changes affecting all LiveComponent instances, including those not using ComponentWithFormTrait. To keep the impact as minimal as possible, I’ve opted for a more targeted fix.

But _you_ did all the hard work here—much appreciated! 🚀

Commits
-------

466c4eb [LiveComponent] Fix ComponentWithFormTrait not working in batch actions
  • Loading branch information
smnandre committed Feb 7, 2025
2 parents 33a4141 + 466c4eb commit feb0256
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/LiveComponent/src/ComponentWithFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,27 @@ public function getFormName(): string
/**
* Reset the form to its initial state, so it can be used again.
*/
private function resetForm(): void
private function resetForm(bool $soft = false): void
{
// prevent the system from trying to submit this reset form
$this->shouldAutoSubmitForm = false;
$this->form = null;
$this->formView = null;
$this->formValues = $this->extractFormValues($this->getFormView());
if (true !== $soft) {
$this->formValues = $this->extractFormValues($this->getFormView());
}
}

private function submitForm(bool $validateAll = true): void
{
if (null !== $this->formView) {
throw new \LogicException('The submitForm() method is being called, but the FormView has already been built. Are you calling $this->getForm() - which creates the FormView - before submitting the form?');
// Two scenarios can cause this:
// 1) Not intended: form was already submitted and validated in the same main request.
// 2) Expected: form was submitted during a sub-request (e.g., a batch action).
//
// Before 2.23, both cases triggered an exception.
// Since 2.23, we reset the form (preserving its values) to handle case 2 correctly.
$this->resetForm(true);
}

$form = $this->getForm();
Expand Down

0 comments on commit feb0256

Please sign in to comment.