1

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.

1 Answers1

0

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(),
        ]);
    }
Bhavin Nakrani
  • 442
  • 3
  • 12
  • My Entity SAVSearch is made of only one variable. It's the variable for the filter: `nom` I also tried the POST method and it doesn't work. And in the profiler, there's no error. In fact, I can see the Request I made. I think the only problem is that it's not passing to the server side: – Badr Al-kebsi Mar 28 '23 at 06:39
  • Please pass form action in this syntax `$form = $this->createForm(SAVSearchType::class, $savSearch);` because you are rendering this method in another page that's why your form is submitted with parent method instead of this `index` method – Bhavin Nakrani Mar 28 '23 at 10:26
  • I see, so I need to handle the form request in the controller that's rendering the parent page instead of this controller? – Badr Al-kebsi Mar 28 '23 at 11:37
  • @BadrAl-kebsi Please check my answer above the code snippet. – Bhavin Nakrani Mar 28 '23 at 12:29
  • Please explain in your answer how this solves the question. Code-only answer are not good answers – DarkBee Mar 29 '23 at 09:56
  • @DarkBee You can check my above comment as an explanation – Bhavin Nakrani Mar 29 '23 at 10:06
  • IMHO the comments are not quite clear – DarkBee Mar 30 '23 at 06:59