0

want to display a form in a modal in the header. In order to make the form work I call the controller Homecontroller.

I called the controller with render controller in the branch but I got a blank page.

Thanks for your help.

header.html.Twig

    <h1 class="fw-bold"></h1>
    <p class="lead fw-bold"></p>
    
      
   
    {{include ('fragments/modal_form.html.twig') }}
   </main>
 </div>
   
</div>

modal_form.html.twig

      {{ render(controller(
       'App\\Controller\\HomeController::index',{'form' : form.createForm()} )) }}
        
     </div>

Controller :

     * @Route("/", name="home")
     */
 
    public function index(PostsRepository $postsRepository,TagRepository $tagRepository, Request $request ):Response
     {
     
     $listTag = $tagRepository->findAll();
      
     $listPost = $postsRepository->findByPostPHp('php');
      
     $posts = $postsRepository->findByExampleField($value = 6);
     $partage = New Posts();
     $form = $this->createForm(PartagePostType::class);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
            $partage = $form->getData();  
            $this->entityManager->persist($partage);
            $this->entityManager->flush();
            $this->addFlash('success', 'Votre post a bien été partagé');
           
        }
     
        return $this->render('home/index.html.twig', [
            'posts' => $posts,
            'tag' => $listTag,
           'listPost' => $listPost,
            'form' => $form->createView(),
            
        ]);
        
    }

1 Answer 1

1

I dont' really get how you are trying to render you form but it doesn't work that way, in your modal_form.html.twig you should use the {{ form_start() }} and {{ form_end() }} twig helpers. They take in parameters the created view of the form, i.e, the variable "form" in your case (created in your render with the createView() method).

It should look like that:

{{ form_start(form}}
    {{ form_row(form.field) }}
    <input type="submit" value="submit">
{{ form_end(form) }}

"field" is whatever name you defined in your FormType. Notice how I added raw HTML for the submit button, it is suggested by Symfony you add the send button that way, even though you can add it in your FormType.

You can learn more about form rendering here : How to Customize Form Rendering

And forms in general there : Forms

Last thing, if you want to use multiple forms with this modal, don't forget to change the name of the variable (also don't forget to add this variable in your controller when you render a template with a form in it, obviously)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.