-
-
Notifications
You must be signed in to change notification settings - Fork 383
[LiveComponent] Better file handling for forms #3111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
[LiveComponent] Better file handling for forms #3111
Conversation
Can you explain a bit more the problem you're solving ? Because this is working right now:
Obviously a file cannot be per se a LiveProp, as it cannot be in HTML, so I don't 100% get how it improves things ? Could you maybe add a test or two, with various situations (one file, multiple files, error on image, error on other field) so we ensure your code change does not impact other behaviour? If needed we can help :) |
to me the problem/issue here is a file bound to a form (via dto or entity mapping), not a direct file send via a live action |
This is a hack because you can't fetch a file from a dto, you have to go manually digging into the request object. The code example I gave currently does not work, but it does on any classic form usage in a form submission through a controller. The main thing here is that it allows for instance to upload files through a form class (with the FileType, or VIchUploadType, etc.). Currently this does not work because the request files are not hydrated into the form values. If we agree on the issue I will gladly add more tests! The number of files does not matter much, it's the very principle behind that is IMO flawed by design if the request is not included in the form submission. |
So i'll be 100% honest and transparent here: in my eyes, one should absolutey never use anything but a dedicated endpoint to upload things. In the vast majority of cases, it leads to either security breach, weird UX (reminder: an upload file does not keep its file after submit, and there's a reason). And weird things on both validation, and ... "rollback" (it do believe it would break with your example..) BUT... obviously [httos://github.com/smnandre' )# love to see an UX Upload around here (that's even the first one I started to draft / big pictures 18 months ago.) Would you like to work on this? It does not need to be easy.... at lease can offer something, right now i cannot |
To be fair, this is not really a matter of opinions: the symfony framework allows handling files inside forms, has always done so, and I don't see any plans on stopping. LiveComponents are presented as the better/modern way of doing forms instead of relying on js for some conditional display, etc for forms. Then they should support the same api, such as file management inside a form :) |
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!