[LiveComponent] Better file handling for forms #3111
+50
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
These changes allow a better file handling for live component forms with files. Currently, the docs require you to handle files directly from the $request object: "The files will be available in a regular
$request->files
files bag". Si if you have a Dto such as :with a corresponding form (text + file inputs), after using $this->submitForm($form) in your live component you would have an empty Dto::file field, and you would be forced to look up inside the $request object. Here I copied and adapted the $form->handleRequest($request) logic you get in a classic controller workflow, wich picks up the files in the request and injects them as data, so now you would do
And at this point you will have $dto::file as an UploadedFile instance you can freely manipulate.
Because
submitForm
has an existing optional parameter the usage feels weird, but to avoid BC break the Request object is to be injected second like$this->submitForm(true, $request)
or as a named parameter$this->submitForm(request: $request)
for proper usage. IMO the two parameters should be swapped in a future major version, I don't think the usage priority of $validateAll precedes the $request handling.This can maybe break the data workflow if you have touched
$this->formValues
somehow because it takes its data from the request directly. I'm not familiar with why this wasn't done this way in the first place so happy to discuss it!