-1

I have a problem with my search, probably I don't understand the principe. So I have a method witch show the products by category, and I added a search to this page for filter products by price. My method is :

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

    $aFilter = array();
    $form = $this->get('form.factory')->createNamedBuilder('search', 'form',  null,  array(
        'csrf_protection' => false,
            ))->setMethod('GET')
                ->add('minimPrice', 'text')
                ->add('maxPrice', 'text')
        ->getForm();
    $request = $this->getRequest();
    $form->handleRequest($request);
    $data = $form->getData();
    print_r($data);

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

My view :

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

The problem is that my $var is empty. Help me please. What I'm doing wrong? Thx in advance

Wouter J
  • 41,455
  • 15
  • 107
  • 112
TanGio
  • 766
  • 2
  • 12
  • 34

1 Answers1

0

If I got you correct this may be the way to go. You need to handle form submission first, then you can get the data for your form fields.

// You need your controller to pass the request
$form->handleRequest($request);

if ($form->isValid()) {
    //Get data from the form and query against the database
    //Render results to template
} else {
  //Form is not submitted or not valid
}

http://symfony.com/doc/current/book/forms.html#handling-form-submissions

Update: After many hours of chatting and finally some remote working with teamviewer. Turned out Nginx was misconfigured and removing query string parameters.

That answer fixed our problem. https://stackoverflow.com/a/21484481/3399234

location / {
   # try_files $uri $uri/ /index.php;
   try_files $uri $uri/ /index.php$is_args$args;
}
Community
  • 1
  • 1
Ugur
  • 1,679
  • 14
  • 20
  • I edited the question by add the $request, but the variable : $data is also empty – TanGio Apr 06 '15 at 12:34
  • I ran the same code on my own Symfony 2.5 environment and it's fine. As documentation states handleRequest method introduced in Symfony 2.3 so it should be working theoretically. Your form is using GET method, do you see the parameters in the url after you submit? e.g: ?search%5BminimPrice%5D=5&search%5BmaxPrice%5D=10 – Ugur Apr 06 '15 at 12:57
  • Yes, I see in the url : ?minimPrice=0&maxPrice=40 – TanGio Apr 06 '15 at 13:05
  • My symfony version is 2.6.5 – TanGio Apr 06 '15 at 13:05
  • The question tagged symfony 2.3 that misguided me. There may be an issue about your request object. Have you checked your request object? According to this answer, http://stackoverflow.com/a/20985212/3399234 getRequest() will be deprecated. You can try to pass your request to your controller this way, public function showCategoryAction(Request $request, $id, $page) – Ugur Apr 06 '15 at 13:14
  • Also don't forget to add use statement to the top. use Symfony\Component\HttpFoundation\Request; – Ugur Apr 06 '15 at 13:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74565/discussion-between-ugur-and-gigel). – Ugur Apr 06 '15 at 13:17