Search code examples
formssymfonytwigrenderedge-side-includes

Why is my form not being submitted when using render_esi Symfony 6


I am making a controller in symfony 6 for a search function and then calling it from my template using the "render_esi" method. The template is rendering the form but when I click on the submit button, nothing happens other than the page being refreshed. Here's the controller:

#[Route('/sav/search', name: 'app_sav_search', methods: ['GET'])]
    public function index(Request $request, ContratRepository $contratRepository): Response
    {
        $savSearch = new SAVSearch;
        $form = $this->createForm(SAVSearchType::class, $savSearch);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            dd('Hi');
        }

        return $this->render('sav_search/_search-form.html.twig', [
            'form' => $form->createView(),
        ]);
    }

I added the dd('hi') just to test if the form is being submitted, and it's not.

here's how im calling it on my template:

{{ render_esi(controller('App\\Controller\\SAVSearchController::index')) }}

I have had this exact problem in the same project somewhere else where I ended up just not using render_esi. But I need to use the search function in multiple page.

Here's also my form:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom', TextType::class, [
                'required' => false,
                'label' => false,
                'attr' => [
                    'placeholder'   => 'Recherchez ici',
                    'class'         => 'form-control search-input mr-sm-2',
                    'aria-label'    => 'Search',
                    'autocomplete'  =>  'off',
                    'spellcheck'    =>  'false',
                ],
            ])
            ->add('submit', SubmitType::class, [
                'attr' => [
                    'class' => 'btn btn-outline-secondary',
                ]
            ])
        ;
    }

and lastly, the form template:

{{ form_start(form, {'attr': {'class': 'row col p-2'}}) }}
    <div class="col">
        {{ form_row(form.nom) }}
    </div>
    <div class="col">
        {{ form_row(form.submit) }}
    </div>
{{ form_end(form) }}

Am I missing something? or should I use another method? I would like any help

PS: the request is being sent, I can see that in my profiler.


Solution

  • Please apply this code to your controller.

        #[Route('/sav/search', name: 'app_sav_search', methods: ['GET', 'POST'])]
        public function index(Request $request, ContratRepository $contratRepository): Response
        {
            $savSearch = new SAVSearch;
            $form = $this->createForm(SAVSearchType::class, $savSearch, 
             ['action' => $this->generateUrl('app_sav_search')]);
    
            $form->handleRequest($request);
            if ($form->isSubmitted() && $form->isValid()) {
                dd('Hi');
            }
    
            return $this->render('sav_search/_search-form.html.twig', [
                'form' => $form->createView(),
            ]);
        }