我想在控制器中提取Symfony2表单的操作。$form->getConfig()->getAction()
只是返回一个空字符串。
还有其他办法吗?
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());
}
例子:
<form action="this/is/the/value/im/interested/in" enctype="...">...</form>
Action为空,因为如果未在表单构建器中显式指定动作,则Symfony表单构建器将使用HTML表单默认行为。当操作为空或未设置时,提交时的默认HTML表单行为是将表单发送到当前URL。
因此,如果要获取该操作的URL,则需要使用一些逻辑:
$action = $form->getConfig()->getAction();
if(empty($action) === true) {
$action = $this->get('request')->getRequestUri();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句