1

I'm trying to set up a form with a FileType field with the multiple option enabled, but if I upload one by one, when submited it only sends the last one to the controller.
I would like to send every file that has been added to the input when submitted. Is there any way to send them?

My form field

class AnadirBarcoFotosForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {   
        $builder              
        ->add('imagenes', FileType::class,[
            'label' => false,
            'multiple' => true,
            'required' => false
        ])
        ->add('Guardar', SubmitType::class, [
            'attr'  =>  [
                'class' => 'btn btn-secondary float-right boton-marron'
            ]
        ])
        ;
    }
   
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([

        ]);
    }
}

Controller


$paramAnadirBarcoFotos = array();
$anadirBarcoFotosForm = $this->createForm(AnadirBarcoFotosForm::class, $paramAnadirBarcoFotos);
$anadirBarcoFotosForm->handleRequest($request);

if ($anadirBarcoFotosForm->isSubmitted() && $anadirBarcoFotosForm->isValid()){
    $datosAnadirBarcoFotos = $anadirBarcoFotosForm->getData();
...
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
  • Can you show your controller method and (relevant parts of) the data class? I think we're missing some details to answer this question. – Stephan Vierkant Jul 22 '21 at 14:45
  • What do you mean the data class, in the form code you can see it has no data class, and in the controller I create the form with an empty array because it doesn't use any entity as data class. When I dump what ->getData() returns, if uploaded one by one, it only returns the last one, when the field has multiple enabled. I think I'm not missing any important detail, but I can be wrong of course. Thank you for your time anyway :) – Cristian Rodriguez Navarro Jul 22 '21 at 18:43
  • This is a matter of debugging. Are you seeing multiple files when dumping $request? – Stephan Vierkant Jul 22 '21 at 18:46
  • When dumping, I see only the last upload, that means, if i upload 1 file and then 5 files at once, I see those last 5 files, but if I upload 3 files one by one, I only recieve the 3rd one – Cristian Rodriguez Navarro Jul 25 '21 at 19:27
  • And what is the result of `$datosAnadirBarcoFotos`? – Stephan Vierkant Jul 25 '21 at 20:10
  • Is an array with the uploaded files, but as i just said, only the last uploaded one as the examples i said in the last comment, anyway i found this question [link](https://stackoverflow.com/questions/29394126/open-several-times-multiple-file-input-without-losing-earlier-selected-files) which says that the input files html tag overwrites the last selected files, so now im trying to find a way to upload multiple files without overwritting – Cristian Rodriguez Navarro Jul 25 '21 at 20:28

0 Answers0