Access POST values in Symfony2 request object

Viewed 285128

OK, this is a newbie question, but I can't find the answer anywhere. In a controller in Symfony2, I want to access the POST value from one of my forms. In the controller I have:

public function indexAction()
{ 
    $request = $this->get('request');
    if ($request->getMethod() == 'POST') {
        $form = $this->get('form.factory')->create(new ContactType());
        $form->bindRequest($request);
        if ($form->isValid()) {
            $name_value = $request->request->get('name');

Unfortunately $name_value isn't returning anything. What am I doing wrong? Thanks!

9 Answers

I think that in order to get the request data, bound and validated by the form object, you must use :

$form->getClientData();

Symfony doc to get request data

Finally, the raw data sent with the request body can be accessed using getContent():

$content = $request->getContent();

Related