Skip to content

Commit 466c4eb

Browse files
committed
[LiveComponent] Fix ComponentWithFormTrait not working in batch actions
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. You did all the hard work here—much appreciated! 🚀
1 parent df6d50b commit 466c4eb

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)