0

I’m having trouble with file uploads in Symfony UX LiveComponent. I’m using Symfony 7.2, Symfony UX 2.x, and PHP 8.2.


The problem

I have a LiveComponent form that includes a file upload field. On the first upload, everything works perfectly — the file is received in the request, saved correctly, and the component re-renders as expected.

However, when I try to upload another file after the re-render, the request no longer includes the file. In the browser’s Network tab (AJAX call: save), the second request doesn’t contain any file in the request.


Code examples

Form Type

<?php

namespace App\Form\AuthorizationRequest;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CreditsRecognitionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('signed_documento_crediti_formativi_c1', FileType::class, [
                'label' => 'Crediti Formativi C1',
                'mapped' => false,
                'attr' => [
                    'data-action' => 'live#action',
                    'data-live-action-param' => 'uploadFile',
                ],
                'required' => true,
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults(['data_class' => null]);
    }
}

LiveComponent method

#[LiveAction]
public function uploadFile()
{
    // Empty method — just triggers an AJAX call
}

Twig template

<div {{ attributes }}>
    <div id="crediti-formativi">
        {{ form_start(form, {
            attr: {
                enctype: 'multipart/form-data',
                class: 'w-100',
            }
        }) }}

        {{ form_widget(form.signed_documento_crediti_formativi_c1) }}

        <div class="w-100 d-flex justify-content-start w-100 align-items-center">
            <button data-loading="action(uploadFile)|addAttribute(disabled)"
                    class="btn-primary btn mt-1 py-2 text-white w-25"
                    data-action="live#action"
                    data-live-action-param="files|save">
                Save
            </button>
        </div>

        {{ form_end(form, { render_rest: false }) }}
    </div>
</div>

What I tried

  • Verified that the form has enctype="multipart/form-data".
  • Checked that the request actually contains files on the first call (it does).
  • Compared the first and second AJAX calls — the second one doesn’t include any file.
  • No JavaScript console errors.

The question

Why does the file upload only work on the first request? Is this a known LiveComponent bug or am I missing some configuration to persist the file input across re-renders?

EDIT:

I've noticed that the "uploadFile" LiveAction, "steals" the files data in the request on the second render, but not in the first one. After removing the "uploadFile" LiveAction, the "__invoke" method built-in in the LiveComponents steals this data. But all of this, only when submitting the second time, never in the first submit....

2
  • Try autcomplete="off" attribute on the input[type="file"]. Commented Oct 8 at 16:49
  • Doesn't seems to work. Commented Oct 9 at 12:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.