Skip to content

Commit feb0256

Browse files
committed
bug #2553 [LiveComponent] Fix ComponentWithFormTrait not working in batch 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
2 parents 33a4141 + 466c4eb commit feb0256

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/LiveComponent/src/ComponentWithFormTrait.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,27 @@ public function getFormName(): string
135135
/**
136136
* Reset the form to its initial state, so it can be used again.
137137
*/
138-
private function resetForm(): void
138+
private function resetForm(bool $soft = false): void
139139
{
140140
// prevent the system from trying to submit this reset form
141141
$this->shouldAutoSubmitForm = false;
142142
$this->form = null;
143143
$this->formView = null;
144-
$this->formValues = $this->extractFormValues($this->getFormView());
144+
if (true !== $soft) {
145+
$this->formValues = $this->extractFormValues($this->getFormView());
146+
}
145147
}
146148

147149
private function submitForm(bool $validateAll = true): void
148150
{
149151
if (null !== $this->formView) {
150-
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?');
152+
// Two scenarios can cause this:
153+
// 1) Not intended: form was already submitted and validated in the same main request.
154+
// 2) Expected: form was submitted during a sub-request (e.g., a batch action).
155+
//
156+
// Before 2.23, both cases triggered an exception.
157+
// Since 2.23, we reset the form (preserving its values) to handle case 2 correctly.
158+
$this->resetForm(true);
151159
}
152160

153161
$form = $this->getForm();

0 commit comments

Comments
 (0)