Symfony 2.3 Form Subscription

I am making a form with Symfony 2.3 to subscribe to a newsletter. The form works well in its own template (newsletter.html.twig).

My controller action:

public function newsletterAction(Request $request)
{
    $newsletter = new Newsletter();

    $form = $this->createFormBuilder($newsletter)
        ->add('email', 'email')
        ->add('submit', 'submit')
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($newsletter);
        $em->flush();

        $request->getSession()->getFlashBag()->add('notice', 'Vous venez de    vous enregistrΓ© Γ  la Newsletter d\'Emoovio.');
    }
    return $this->render('MyBundle:Global:newsletter.html.twig', array(
        'form' => $form->createView(),
    ));
}

      

My template where it works (newsletter.html.twig):

{{ form(form) }}

      

My template where it fails (index.html.twig):

////
{% render (controller("EmooviofrontBundle:Global:newsletter")) %}
////

      

The form is displayed, but it doesn't work. Maybe something is missing. Someone had the same problem and could explain it to me. Thank.

+3


source to share


1 answer


I think that when you submit your form, the POST data goes to indexAction. This action does not validate your form and displays the normal page. During rendering, it will call Global: newsletterAction, but only as an additional request in a GET method without form data.

You can try to apply an action to your forms



    $form = $this->createFormBuilder($newsletter, array(
        'action' => $this->generateUrl('global_newsletter'),
        'method' => 'POST',
    ))
   ->add('email', 'email')
   ->add('submit', 'submit')
   ->getForm();

      

0


source







All Articles