3

I am trying to embed a controller in a Twig template like this:

{{ render(controller('IRCGlobalBundle:MailingList:join')) }}

This controller renders a basic form to join a mailing list:

namespace IRC\GlobalBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use IRC\GlobalBundle\Entity\MailingList;
use IRC\GlobalBundle\Form\Type\MailingListType;

class MailingListController extends BaseController
{
    public function joinAction(Request $request) {
        $this->getSiteFromRequest($request);
        $mailingList = new MailingList;
        $mailingList->setSite($this->site);
        $form = $this->createForm(new MailingListType(), $mailingList);

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            echo "submitted form";
        } else {
            echo "unsubmitted form";
        }

        return $this->render('IRCGlobalBundle:Forms:join_mailing_list.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

The problem is that the $form->isSubmitted() method never returns true so the form submission cannot be validated/handled. What am I doing wrong? Do I need to change the target of the form to point to my embedded controller?

1 Answer 1

2

I guess it's because the form will be rendered like this <form action=""> so it will post the information to the same page but since you are rendering the form using a sub-request the new sub-request will not carry any form data.

The easiest way to solve this issue is by hacking the form action attribute and make it send the request to joinAction and you can redirect or forward the request after handling the form to the page where the user came from.

something like this should work:

$form = $this->createForm(new MailingListType(), $mailingList, array(
     'action'   => //generate a url to joinAction
));
Sign up to request clarification or add additional context in comments.

1 Comment

How to generate the url since the joinAction does not have Routing ?

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.