从symfony2中的表单中获取数据

时间:2015-04-06 12:11:21

标签: php symfony symfony-forms php-5.3 symfony-2.3

我的搜索有问题,可能我不懂原理。所以我有一个按类别显示产品的方法,我在这个页面上添加了一个按价格搜索过滤产品的方法。我的方法是:

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

我的观点:

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

1 个答案:

答案 0 :(得分:0)

如果我纠正你,这可能就是你要走的路。 您需要先处理表单提交,然后才能获取表单字段的数据。

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

更新: 经过几个小时的聊天,最后与团队观察员进行了一些远程合作。 原来Nginx配置错误并删除了查询字符串参数。

那个答案解决了我们的问题。 https://stackoverflow.com/a/21484481/3399234

location / {
   # try_files $uri $uri/ /index.php;
   try_files $uri $uri/ /index.php$is_args$args;
}