1

In my base twig file I'm rendering my footer. In the footer these is an registration form for subscripting to the newsletter. This is de render call in the base twig.

{{ render(controller('MyBundle:Global:footer')) }}

Via this footer controller I'm rendering the footer. Hereby the controller code:

 /**
 * @Route("/{_locale}/newsletter/", defaults={"_locale": "nl"}, requirements={"_locale": "nl|en|de"}, name="_newsletter")
 */
public function footerAction(Request $request)
{
    $form = $this->createForm(new NewsletterType());
    $form->handleRequest($request);

    if ($form->isValid()) {
        return $this->redirectToRoute('_404');
    } else {
        return $this->render('MyBundle:global:footer.html.twig', array('form' => $form->createView()));
    }
}

If I submit the form what only a email input is and an submit button then this route is triggered, only the form is not validated. In this example for test I want to redirect it to the 404 page. But it just reders the footer only?

1

1 Answer 1

2

I assume that you render this form on many pages because you place it in the footer. As any form you need an action attribute in the form element e.g

<form method="post" action="somewhere">

And the value of the action attribute is where your data will arrive if someone hits the Send button.

My Solution is to add an extra page that shows the same form. Like you are used to with Symfony and than render the same form in the footer and be sure that your form submits to the new page with the same form. e.g.

$form->setAction($this->generateUrl('target_route'))

Now if anybody submits the form it will be sent to the page with the same form and if there are any errors he will see them on this page.

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.