Bob
Bob

Reputation: 67

Get 'action' property of a Symfony2 form in controller

I'd like to extract the action of a Symfony2 form in my controller. $form->getConfig()->getAction() just returns an empty string.

Is there any other way?

public function fooAction(Request $request)
{
    $bar = new BarEntity();
    $form = $this->createForm(new BarType(), $bar);

    $form->handleRequest($request);

    $action = ''; // how to get the action?

    return array('form' => $form->createView());
 }

Example:

 <form action="this/is/the/value/im/interested/in" enctype="...">...</form>

Upvotes: 1

Views: 722

Answers (1)

LPodolski
LPodolski

Reputation: 2878

Action is empty, because if action is not explicitly specified in form builder, Symfony form builder use HTML form default behavior. Default HTML form behavior on submit when action is empty or not set is that form is send to current URL.

So if you want get URL of that action you need use some logic:

$action = $form->getConfig()->getAction();
if(empty($action) === true) {
     $action = $this->get('request')->getRequestUri();
}

Upvotes: 1

Related Questions