Variable "formUser" does not exist. So it does exist ! Problem communicating between files?
10:54 10 Dec 2025

I have this error: Variable “formUser” does not exist. And I don't understand where it comes from because on the same page I already have a form that worked fine before I made this one.

In my pages/signalement/index.html.twig page, I have this:

{% include 'pages/user/_csv.html.twig' with {form: formUser} only %}

In this include, I just have some basic code:

{% if form is defined %}
{{ form_start(formUser)}}
{{ form_end(formUser)}}
{% endif %}

In the UserController controller, I have this:

#[Route('/signalement', name: 'signalement.index', methods:['GET', 'POST'])]
    public function upload(
        Request $request,
        EntityManagerInterface $manager
        //  #[Autowire('%kernel.project_dir%/public/uploads/users')] string $fileDirectory
    ) : Response {
            $upload = new Upload();
            $formUser = $this->createForm(ImportType::class, $upload);
            $formUser->handleRequest($request);
            if ($formUser->isSubmitted() && $formUser->isValid()) {

                $file = $upload -> getCsvFile();
                $fileName = md5(uniqid()).'.'.$file->guessExtension();
                $file->move($this->getParameter('csvFile', $fileName));
                $upload->setCsvFile($fileName);

                $csv = Reader::createFromPath('%kernel.root.dir%/../public/uploads/'.$fileName);
                $csv->setHeaderOffset(0);
                $csv->setDelimiter(';');
                $csv->setEscape('');
                $records = $csv->getRecords();
                foreach ($records as $record){ 
                    $user = (new User())
                        ->setNom($record['nom'])
                        ->setPrenom($record['prenom'])
                        ->setEmail($record['email'])              
                        ->setPlainPassword($record['password'])
                    ;
                    $this->em->persist($user);
                }
                $this->em->flush();
                $this->addFlash('success','Good !!');
                return $this->redirectToRoute('signalement.index');
            }
            return $this->render('pages/signalement/index.html.twig', [
                'form' => $formUser,
            ]);
    }

This one uses the Entity Upload:

#[ORM\Entity(repositoryClass: UploadRepository::class)]
class Upload
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $csvFile = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCsvFile(): ?string
    {
        return $this->csvFile;
    }

    public function setCsvFile(?string $csvFile): static
    {
        $this->csvFile = $csvFile;
        return $this;
    }
}

and the ImportType:

class ImportType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('csvFile', FileType::class,[
                'label' => 'Fichier CSV',
                'mapped' => false,
                'required' => false,
                'constraints' => [
                    new File(
                        maxSize: '1024k',
                        extensions: ['csv'],
                        extensionsMessage: 'Insert a valid .csv file',
                    )
                ],
            ])
              ->add('submit', SubmitType::class, [
                'attr' => ['class' => 'btn btn-primary mt-4 valider'],
                'label' => 'Insert CSV'
            ])
        ;
    }

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

This means there are a lot of things going on, and I feel like I've gotten myself a little mixed up! On this same page, I already have a filter form that works very well when I comment out everything related to my CSV insertion form! I've obviously tried lots of things and looked at lots of posts here and elsewhere, and I've been stuck on this for a few days now! Thank you for your help :)

symfony