0

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
                        )

                )

        )
3
  • $request->get('name') Commented May 26, 2014 at 13:49
  • Didn't print anything. No error. Commented May 26, 2014 at 13:52
  • You can also dump whole $request object and look where it store post values. Commented May 26, 2014 at 13:53

2 Answers 2

1

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

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

7 Comments

Error: Call to undefined method Symfony\Component\HttpFoundation\Request::request()
Sorry, it isn't a method it's an instance of ParameterBag, fixed the answer.
Error gone away but no data printed. echo $request->request->get('name'); exit;
Which means there is no 'name' POST parameter. Use debug toolbar to check what is in your request.
There is, check updated post above please. $request dump shows it.
|
0

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

2 Comments

I've just created an example like that. It is a good way as well.
It gives you all the options for build in validation, code re-usability and protection like CSRF-protection. That's why you should use it over the suggested solution above. The time spend is low and the benefits are very high :)

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.