Skip to content

Commit b8aa233

Browse files
Implemented form handling for POST forms in Live Components
1 parent 5966758 commit b8aa233

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/LiveComponent/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"symfony/expression-language": "^5.4|^6.0|^7.0|^8.0",
4646
"symfony/form": "^5.4|^6.0|^7.0|^8.0",
4747
"symfony/framework-bundle": "^5.4|^6.0|^7.0|^8.0",
48+
"symfony/http-foundation": "^5.4|^6.0|^7.0|^8.0",
4849
"symfony/options-resolver": "^5.4|^6.0|^7.0|^8.0",
4950
"symfony/phpunit-bridge": "^6.1|^7.0|^8.0",
5051
"symfony/security-bundle": "^5.4|^6.0|^7.0|^8.0",

src/LiveComponent/src/ComponentWithFormTrait.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
1515
use Symfony\Component\Form\ClearableErrorsInterface;
16+
use Symfony\Component\Form\FormError;
1617
use Symfony\Component\Form\FormInterface;
1718
use Symfony\Component\Form\FormView;
19+
use Symfony\Component\Form\Util\FormUtil;
20+
use Symfony\Component\Form\Util\ServerParams;
21+
use Symfony\Component\HttpFoundation\Request;
1822
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
1923
use Symfony\Contracts\Translation\TranslatableInterface;
2024
use Symfony\UX\LiveComponent\Attribute\LiveProp;
@@ -147,7 +151,7 @@ private function resetForm(bool $soft = false): void
147151
}
148152
}
149153

150-
private function submitForm(bool $validateAll = true): void
154+
private function submitForm(bool $validateAll = true, ?Request $request = null): void
151155
{
152156
if (null !== $this->formView) {
153157
// Two scenarios can cause this:
@@ -160,7 +164,50 @@ private function submitForm(bool $validateAll = true): void
160164
}
161165

162166
$form = $this->getForm();
163-
$form->submit($this->formValues);
167+
$method = $form->getConfig()->getMethod();
168+
169+
// For request methods that must not have a request body we fetch data
170+
// from the query string. Otherwise we look for data in the request body.
171+
if ('POST' === $method && $request instanceof Request) {
172+
// Mark the form with an error if the uploaded size was too large
173+
// This is done here and not in FormValidator because $_POST is
174+
// empty when that error occurs. Hence the form is never submitted.
175+
$serverParams = new ServerParams();
176+
if ($serverParams->hasPostMaxSizeBeenExceeded()) {
177+
// Submit the form, but don't clear the default values
178+
$form->submit(null, false);
179+
180+
$form->addError(new FormError(
181+
$form->getConfig()->getOption('upload_max_size_message')(),
182+
null,
183+
['{{ max }}' => $serverParams->getNormalizedIniPostMaxSize()]
184+
));
185+
186+
return;
187+
}
188+
189+
$name = $form->getName();
190+
191+
if ('' === $name) {
192+
$params = $request->request->all();
193+
$files = $request->files->all();
194+
} elseif ($request->request->has($name) || $request->files->has($name)) {
195+
$default = $form->getConfig()->getCompound() ? [] : null;
196+
$params = $request->request->all()[$name] ?? $default;
197+
$files = $request->files->get($name, $default);
198+
} else {
199+
// Don't submit the form if it is not present in the request
200+
return;
201+
}
202+
203+
if (\is_array($params) && \is_array($files)) {
204+
$data = FormUtil::mergeParamsAndFiles($params, $files);
205+
} else {
206+
$data = $params ?: $files;
207+
}
208+
}
209+
210+
$form->submit($data ?? $this->formValues, $request);
164211
$this->shouldAutoSubmitForm = false;
165212

166213
if ($validateAll) {

0 commit comments

Comments
 (0)