Search code examples
twigsymfony-formssymfony6

Symfony 6 form trouble


Currently working on a new web project running under Symfony 6. Unfortunately, I just caught this error :

Variable "ajouterNouveauProjet" does not exist.

By the way, I cannot find the issue ! Here is my controller code :

<?php

namespace App\Controller;

use App\Entity\Projet;
use App\Form\ProjetFormType;
use Doctrine\ORM\EntityManagerInterface;
use phpDocumentor\Reflection\Types\This;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class LandingPageController extends AbstractController
{
    #[Route('/landingPage', name: 'app_landing_page')]
    public function index(): Response
    {
        return $this->render('landing_page/index.html.twig', [
            'controller_name' => 'LandingPageController',
        ]);
    }
    #[Route('/projet', name: 'projet')]
    public function projet(Request $request, EntityManagerInterface $em, ): Response
    {
        //On créé un nouveau produit
        $projet = new Projet();

        $addProjetForm = $this->createForm(ProjetFormType::class, $projet);

        return $this->render('landing_page/projet.html.twig', [
            'ajouterNouveauProjet' => $addProjetForm->createView(),
        ]);

    }
}

and here you can find my view code :

{% extends 'base.html.twig' %}

{% block title %}Projet{% endblock %}

{% block body %}

    {% include "landing_page/_menuDashboard.html.twig" %}


    <form class=" box " method="post">

        <h1 class="title is-1">Mes projets</h1>

        {{ form_start(ajouterNouveauProjet) }}

        <div class="columns">

            <div class="column">

                {{ form_row(ajouterNouveauProjet.raisonSociale,
                    {
                        label: 'Raison Sociale',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.activite,
                    {
                        label: 'Activité',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.responsable,
                    {
                        label: 'Responsable',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.formeJuridique,
                    {
                        label: 'Forme Juridique',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.typeJuridique,
                    {
                        label: 'Type juridique',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.mail,
                    {
                        label: 'Adresse E-mail',
                        attr: {class: 'input form-control'}
                    })
                }}

            </div>

            <div class="column">

                {{ form_row(ajouterNouveauProjet.capital,
                    {
                        label: 'Capital',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.adresse,
                    {
                        label: 'Adresse',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.codePostal,
                    {
                        label: 'Code Postal',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.ville,
                    {
                        label: 'Ville',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.telephone,
                    {
                        label: 'Téléphone',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.siret,
                    {
                        label: 'SIRET',
                        attr: {class: 'input form-control'}
                    })
                }}

                {{ form_row(ajouterNouveauProjet.naf,
                    {
                        label: 'NAF',
                        attr: {class: 'input form-control'}
                    })
                }}

            </div>

        </div>

        <button type="submit" class="button is-info">Valider</button>

        {{ form_end(ajouterNouveauProjet) }}

    </form>

{% endblock %}

Someone got an idea why I have this error ? everything seems to be OK !

Have a great day guys !

I Try to rename my variables and move my folder root.


Solution

  • Try to remove ->createView() in the controller.

    If you are using symfony 6.3+ render() is fine. otherwise use renderForm()

    In Symfony 6.3 the render() function has changed. The render() method calls $form->createView() to transform the form into a form view instance.


    Symfony 6.3+

    public function projet(Request $request, EntityManagerInterface $em, ): Response
    {
        //On créé un nouveau produit
        $projet = new Projet();
    
        $addProjetForm = $this->createForm(ProjetFormType::class, $projet);
    
        return $this->render('landing_page/projet.html.twig', [
            'ajouterNouveauProjet' => $addProjetForm,
        ]);
    
    }
    

    symfony <= 6.2

    #[Route('/projet', name: 'projet')]
    public function projet(Request $request, EntityManagerInterface $em, ): Response
    {
         //On créé un nouveau produit
         $projet = new Projet();
    
         $addProjetForm = $this->createForm(ProjetFormType::class, $projet);
    
         return $this->renderForm('landing_page/projet.html.twig', [
             'ajouterNouveauProjet' => $addProjetForm,
         ]);
    
    }