Skip to content

[Bug] File upload lost in subsequent AJAX requests after LiveComponent re-render #3231

@woweya

Description

@woweya

When using LiveComponent with file upload fields, after a validation error causes the component to re-render, the file input loses its binding to the correct LiveAction. The file is then sent in the next unrelated AJAX request (e.g., a text field change) instead of the final form submission.

Environment

  • Symfony: 7.2
  • Symfony UX LiveComponent: [2.x]
  • PHP: 8.2

Steps to Reproduce

  1. Create a LiveComponent with a form containing:

    • Text fields
    • A file upload field (DocumentType or similar)
    • Validation constraints on the file (e.g., mime type, size)
  2. Submit the form with an invalid file (triggers validation error)

  3. Component re-renders showing validation errors

  4. Upload a new valid file after re-render

  5. Modify a text field (triggers AJAX request for live update)

  6. Submit the form

Expected Behavior

  • The file should be sent only with the final form submission (LiveAction save)
  • Text field changes should not include the file in their AJAX payload

Actual Behavior

  • After re-render, the file is sent with the first AJAX request after selecting it (e.g., text field blur/change event)
  • The final form submission does not include the file
  • The file input appears to be "consumed" by the wrong request

STEP 0: Submit form → Validation Error (file invalid) → Component re-renders
STEP 1: Select new file in file input
STEP 2: Modify a text field → AJAX request fires → FILE IS SENT HERE (wrong!)
STEP 3: Click Submit → AJAX request fires → FILE IS MISSING

Code Example

LiveComponent:
In my php file:

#[AsLiveComponent('TestLiveComponent', template: '...')]
class TestLiveComponent extends AbstractController
{
    use DefaultActionTrait;
    use ComponentWithFormTrait;

    #[LiveAction]
    public function save(Request $request): Response
    {
        $this->submitForm();
        $form = $this->getForm();
        
        // File is NULL here after re-render + new file selection
        $uploadedFile = $request->files->get('my_form_document_type')['document'] ?? null;
        // ...
    }
}

In my twig file, simply we have:

    {{ form_widget(form.document) }}

Workaround Attempted

Manually resetting file input after re-render: Did not work
Using data-live-ignore on file input: Prevents re-render but breaks validation display

So, I would ask to you:
Is there a recommended pattern for handling file uploads with validation errors in LiveComponent?
Should file inputs be excluded from live updates and only processed on explicit form submission?
Is there a way to preserve file input state across re-renders?

Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions