BentCoder
BentCoder

Reputation: 12730

Handling form submission in another method

I want to get submission data in sendAction() below but I can't. Examples in documentation are all done in a single method. I'm generating form in indexAction() and handling submission in sendAction().

There are many examples on web as well as in Stackowerflow but for some reason there is no example like mine, or maybe I missed.

Thanks for the help

namespace Se\HirBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class EmailController extends Controller
{
    public function indexAction()
    {
        $defaultData = array('message' => 'Type your message here');

        $form = $this->createFormBuilder($defaultData)
                ->setAction($this->generateUrl('email_send'))
                ->setMethod('POST')
                ->add('name', 'text')
                ->add('email', 'email')
                ->add('message', 'textarea')
                ->add('send', 'submit')
                ->getForm();

        return $this->render('SeHirBundle:Default:email.html.twig',
                            array('page' => 'Email', 'form' => $form->createView()));
    }

    public function sendAction(Request $request)
    {
        if ($request->getMethod() == 'POST')
        {
            $name = //How to get submission data
            $email = //How to get submission data
            $message = //How to get submission data

            return new Response('Email sent');
        }

        return new Response('Form is faulty');
    }
}

This prints nothing as well: $this->get('request')->request->get('name');

When I dump $request, I get this:

Symfony\Component\HttpFoundation\Request Object
(
    [attributes] => Symfony\Component\HttpFoundation\ParameterBag Object
        (
            [parameters:protected] => Array
                (
                    [_controller] => Se\HirBundle\Controller\EmailController::sendAction
                    [_route] => email_send
                    [_route_params] => Array
                        (
                        )

                )

        )

    [request] => Symfony\Component\HttpFoundation\ParameterBag Object
        (
            [parameters:protected] => Array
                (
                    [form] => Array
                        (
                            [name] => 11
                            [email] => [email protected]
                            [message] => Type your message here
                            [send] => 
                            [_token] => NfPK_MN6oWYQm2SRrgRjoldwkrodiT033FNsXSjv3TA
                        )

                )

        )

Upvotes: 0

Views: 169

Answers (2)

ferdynator
ferdynator

Reputation: 6410

Best way would be to create your own form class and use it in both actions. Once for generation and once for validation.

Upvotes: 0

Igor Pantović
Igor Pantović

Reputation: 9246

If you want to pull POST data directly from request you can do $request->request->get('name').

Upvotes: 1

Related Questions