我試圖創建一個使用Symfony的2.5使用this教程的形式,但本教程是使用舊版本的Symfony的。我可以獲取表單來顯示和創建實體,但是我現在正在提交表單。以下是從教程中的代碼,這代碼是默認的控制器內contactAction
創建聯繫表使用的Symfony
public function contactAction()
{
$enquiry = new Enquiry();
$form = $this->createForm(new EnquiryType(), $enquiry);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// Perform some action, such as sending an email
// Redirect - This is important to prevent users re-posting
// the form if they refresh the page
return $this->redirect($this->generateUrl('BloggerBlogBundle_contact'));
}
}
return $this->render('BloggerBlogBundle:Page:contact.html.twig', array(
'form' => $form->createView()
));
}
我的主要關注點是在上面的代碼
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
下一節正如你會發現它是用getRequest()
這一直depricated然後我的IDE告訴我buildRequest
方法無法找到。
我真的很感激,如果有人打電話給我轉換爲symfony版本2.5的contactAction
的正確路徑,我將非常感激。
的[官方文檔](http://symfony.com/doc/current/book/forms.html)提供幾乎相同的情況。 – andy 2014-09-22 12:36:41
答案的請求部分正確。只有@MatthewThomas表明你不再需要檢查POST。但是,他的解決方案使用的註釋可能會讓您感到困惑。 – Cerad 2014-09-22 12:51:15
感謝大家的反饋,我正在通過答案並試圖理解他們,因爲我正在創建表單,它不做任何事情,所以我也想知道如何檢查提交的表單數據?而使用普通的PHP print_r($ _ POST);會顯示錶單數據,我如何查看使用symfony? – Baig 2014-09-22 12:55:39