2015-04-07 22 views
0

數據我有一個方法:如何從一種形式獲得的Symfony2

public function showCategoryAction($id, $page, Request $request){ 
    $em = $this->getDoctrine()->getManager(); 
    $repositoryProduct = $em->getRepository('ShopDesktopBundle:Product'); 

    $aFilter = array(); 
    $form = $this->get('form.factory')->createNamedBuilder('', 'form', null, array(
         'csrf_protection' => false, 
      )) 
      ->setMethod('GET') 
      ->add('minimPrice', 'text', array('mapped' => false, 'label' => 'De la :' , 'attr'=> 
             array(
              'placeholder'=>'Minim price', 
              'class'=>'form-control'))) 
      ->add('maxPrice', 'text',array('mapped' => false, 'label' => 'Pina la :' , 'attr'=> 
             array(
              'placeholder'=>'Max price', 
              'class'=>'form-control'))) 
    ->getForm(); 

    $form->handleRequest($request); 
    $var = $form->get('minimPrice')->getData(); 
    print_r($var); 
    //Search products 
    $aProducts   = $repositoryProduct->getProductsOrderByDateDesc($id,null,$aFilter); 
    if (!$aProducts) { 
     throw $this->createNotFoundException('Products not found.'); 
    } 

    $category = $em->getRepository('ShopDesktopBundle:Category')->findOneById($id); 
    if (!$category) { 
     throw $this->createNotFoundException('Category not found.'); 
    } 
    //Create pagination 
    $paginator = $this->get('knp_paginator'); 
    $pagination = $paginator->paginate(
     $aProducts, 
     $page, 
     3 
    ); 

    //Send data to view 
    return $this->render('ShopDesktopBundle:Category:category.html.twig',array(
     'category'   => $category, 
     'pagination'  => $pagination, 
     'form' => $form->createView() 
    )); 
} 

我的觀點:

<form action="{{ path('show_product_category',{ 'id':category.getId(), 'name':category.getCategoryLink() }) }}" method="get" {{ form_enctype(form) }}> 
    {{ form_widget(form) }} 
    <input type="submit" class="btn btn-primary marg-left-20" value="Search"/> 
</form> 

我搜索,通常一切正常,但我是$ var變量爲空。我不明白我的問題在哪裏,可能我錯過了一些東西。創建未在控制器中映射的表單是一個好主意?請幫幫我。 THX提前

+0

我猜方法應該是交沒有得到樹枝文件 –

+0

我試過了,不工作 – TanGio

+0

方法應該在你的表格中發佈,即使它不工作,第一個問題,$ formData = $ form-> getData();回報什麼? $ request-> request-> all()會返回什麼? –

回答

0
 if ('POST' === $request->getMethod()) 
    { 
     $form->bindRequest($request); //Symfony 2.0.x 
     //$form->bind($request); //Symfony 2.1.x 

     $name = $form->get('name')->getData(); 
    } 

我不上岸,但是這應該爲你工作

0

如果您正在使用的Symfony 2.3,你可以這樣來做:

public function showCategoryAction($id, $page, Request $request) { 
    //... 
    $form = // whatever... 

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

     if ($form->isValid()) 
     { 
      // Do your magic! 
      // Persist your form, send email, blablabla... 
      return $this->redirect($this->generateUrl('your_url_to_show')); 
     } 
    } 

    return $this->render(/*same code you have...*/); 
} 

此外,如果我沒有按「T工作或$request是空的,你也可以得到$request以另一種方式:

public function showCategoryAction($id, $page) { 
    $request = $this->get('request'); 
    //... 
}