2014-09-22 100 views
-1

我試圖創建一個使用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的正確路徑,我將非常感激。

+0

的[官方文檔](http://symfony.com/doc/current/book/forms.html)提供幾乎相同的情況。 – andy 2014-09-22 12:36:41

+0

答案的請求部分正確。只有@MatthewThomas表明你不再需要檢查POST。但是,他的解決方案使用的註釋可能會讓您感到困惑。 – Cerad 2014-09-22 12:51:15

+0

感謝大家的反饋,我正在通過答案並試圖理解他們,因爲我正在創建表單,它不做任何事情,所以我也想知道如何檢查提交的表單數據?而使用普通的PHP print_r($ _ POST);會顯示錶單數據,我如何查看使用symfony? – Baig 2014-09-22 12:55:39

回答

1

嗨有幾個不贊成的電話,我真的會推薦去Symfony的食譜。但無論如何,這下面會有所幫助。

namespace myproject/mybundle/controller; 

use Symfony\Component\HttpFoundation\Request; 

Class Act //that is me ;) { 

    /** 
    * @Route("/contact", name="_lalalalala") 
    * @Template() 
    */ 
    public function contactAction(Request $request){ 
    $enquiry = new Enquiry(); 
    $form = $this->createForm(new EnquiryType(), $enquiry); 
    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($enquiry); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('BloggerBlogBundle_contact')); 
    } 

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

您可以通過使用symfony服務容器來注入表單來進一步縮短此代碼。我會建議閱讀這是非常棒的。正如你可以重新在任何地方使用形式:)

2

聲明是這樣的動作:

public function contactAction(Request $request) 
{ 
... 
} 

和進口:

use Symfony\Component\HttpFoundation\Request; 

,你將有你的行動裏的要求,這樣你就可以刪除此行:

$request = $this->getRequest(); 
+0

我如何檢查提交的變量?而使用普通的PHP'print_r($ _ POST);'會顯示錶單數據,我如何查看使用symfony作爲當前的表單不做任何事情,我想檢查它是否提交數據 – Baig 2014-09-22 12:51:55

+0

如果你想打印POST變量,執行此操作:var_dump($ request-> request-> all()); – tomazahlin 2014-09-22 12:58:23

0

您可以更改getMethodisMethod

if ($request->isMethod('POST')) 

然後您可以將請求數據提交給使用submit

$form->submit($request->request->get($form->getName())); 

或者形式您可以使用handleRequest方法將處理上述2方法一氣呵成,然後就可以像

有你的行動休息繼續前行
$form->handleRequest($request); 

if ($form->isValid()) 
    .. etc 
0

要檢索請求對象,你有兩種可能性。或者讓Symfony pass the request as an argument to your controller action ...

public function contactAction(Request $request) 
{ 
    // ... 
} 

...或者從容器中獲取請求對象。

$request = $this->get('request'); 

對於結合的請求的形式的,the manual states以下:

將請求傳遞直接提交()仍然有效,但不建議使用並會在Symfony的3.0中被移除。您應該使用方法handleRequest()

這種方式,請求結合部將類似於此:

if ($request->isMethod('POST')) { 
    $form->handleRequest($request); 

    if ($form->isValid()) { 
     // do something 
    } 
}