Skip to content

Conversation

florentdestremau
Copy link
Contributor

Q A
Bug fix? no, but this feels like a bugfix to me
New feature? yes
Deprecations? no
Documentation? needed
Issues Fix #...
License MIT

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 :

class Dto {
  public string $name;
  public ?UploadedFile $file;
}

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

#[LiveAction]
public function save(Request $request) {
  $this->submitForm(request: $request);
  $dto = $this->getForm()->getData();

  $dto->file->move('public/uploads');   // yay $dto->file is an UploadedFile now !
}

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!

@carsonbot carsonbot added Feature New Feature LiveComponent Status: Needs Review Needs to be reviewed labels Sep 29, 2025
@smnandre
Copy link
Member

smnandre commented Oct 3, 2025

Can you explain a bit more the problem you're solving ?

Because this is working right now:

#[LiveAction]
public function myAction(Request $request): void
{
    $file = $request->files->get('my_file');
}

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 :)

@smnandre smnandre added Status: Waiting feedback Needs feedback from the author and removed Status: Needs Review Needs to be reviewed labels Oct 3, 2025
@94noni
Copy link
Contributor

94noni commented Oct 3, 2025

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
i had also once an issue, and I used a workaround to send the file after the form

@florentdestremau
Copy link
Contributor Author

florentdestremau commented Oct 3, 2025

Can you explain a bit more the problem you're solving ?

Because this is working right now:

#[LiveAction]
public function myAction(Request $request): void
{
    $file = $request->files->get('my_file');
}

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.

@smnandre
Copy link
Member

smnandre commented Oct 5, 2025

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
ok-j :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature LiveComponent Status: Waiting feedback Needs feedback from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants