[英]Symfony2 render and form
我有一個要在網站的多個頁面中使用的表格。 所以我在頁面中調用類似的方法:
{{ render(controller('ProjectApplicationBundle:Application:form')) }}
我有formAction方法:
public function formAction()
{
$form = $this->createForm($type,$obj);
if('POST' == $this->getRequest()->getMethod()){
$form->submit($this->getRequest());
if($form->isValid()){
//redirect to a page after the form
}
}
//render the form template ...
}
這不是我的代碼,這只是一個示例。
所以我添加了一條創建發送表格的路線
project_application_form:
pattern: /form
defaults: { _controller: ProjectApplicationBundle:Application:form }
現在我的問題:)
1)我想使用url阻止對表單的訪問,只允許進行數據處理。
2)如果出現錯誤,頁面將不會重定向,因此顯示的表單沒有布局,因此我想在呈現表單的頁面中獲取帶有錯誤的表單。
編輯:我也許有第二點的解決方案,但這不是很適當。 在我的渲染路徑的參數中設置並返回該路徑,而不是渲染渲染表單。 最后,我使用一個會話來放置錯誤。
我希望這對你來說很清楚,對不起我的英語。
您必須保護處理表單的操作,並僅通過POST
使它可用
范例:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class RegisterController extends Controller
{
/**
* @Route("/register/process", name="process_form_register")
* @Method({"POST"})
*/
public function processAction(Request $request)
{
$form = $this->createFrom(new RegisterType());
$form->submit($request);
if($form->isValid()){
//redirect to a page after the form
}
}
}
如果對路由使用yml
語法,它將為:
process_form_register:
pattern: /register/process
defaults: { _controller: ...... }
requirements:
_method: POST
為您的2)點打開一個新問題,因為每個線程只有一個問題,這是另一個問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.