我想弄清楚如何在Symfony 2中使用动态表单/表单类型。我已经管理到目前为止,但我不知道也找不到的是如何在POstrongUBMIT事件后重定向。
我是在控制器内做还是直接在表单类型类中发生?现在发生的情况是页面重新加载,但页面中的数据没有更新。另外,我想重定向到另一个页面。
From the doc:
if ($form->isValid()) {
// perform some action, such as saving the task to the database
return $this->redirect($this->generateUrl('task_success'));
}
isValid()
方法只有在POST
请求被执行并且发送的数据是有效的情况下才会返回true。
如果你想在POST
请求后重定向,即使在表单无效的情况下,你可以这样做:
public function newAction(Request $request)
{
// ...
if ($request->isMethod('POST')) {
if ($form->isValid()) {
// perform some action...
}
return $this->redirect($this->generateUrl('page_after_post_request'));
}
// ...
}