2015-04-07 2 views
0

J'ai une méthode:Comment obtenir des données d'un formulaire dans 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() 
    )); 
} 

Mon point de vue:

<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> 

Je recherche et normalement tout est ok, mais ma variable $ var est nul. Je ne comprends pas où est mon problème, probablement je manque quelque chose. C'est une bonne idée de créer des formulaires qui ne sont pas mappés dans le contrôleur. Aidez-moi, s'il vous plaît. Thx à l'avance

+0

Je suppose que la méthode devrait être post pas obtenir dans le fichier twig –

+0

J'ai essayé, ne fonctionne pas – TanGio

+0

La méthode devrait être post dans votre formulaire même si cela ne fonctionne pas que le premier problème, fait $ formData = $ form-> getData(); retourner quelque chose? le $ request-> request-> all() renvoie-t-il quelque chose? –

Répondre

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

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

Je ne suis pas le rivage, mais cela devrait fonctionner pour vous

0

Si vous utilisez Symfony 2.3, vous pouvez le faire de cette façon:

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...*/); 
} 

En outre, dans le cas où je doesn « t travail ou $request est vide, vous pouvez également obtenir le $request d'une autre manière:

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