【问题标题】:Get data from a form in symfony2从 symfony2 中的表单获取数据
【发布时间】:2015-04-06 12:11:21
【问题描述】:

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

 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 是空的。请帮帮我。我做错了什么?提前谢谢

【问题讨论】:

  • 你必须用你的 $form 处理Request 来获取数据,查看 Symfony 文档

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


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 我通过添加 $request 来编辑问题,但变量:$data 也是空的
  • 我在自己的 Symfony 2.5 环境中运行了相同的代码,这很好。正如文档所述,Symfony 2.3 中引入了 handleRequest 方法,因此理论上它应该可以工作。您的表单使用的是GET方法,您提交后是否看到url中的参数?例如:?search%5BminimPrice%5D=5&search%5BmaxPrice%5D=10
  • 是的,我在网址中看到:?minimPrice=0&maxPrice=40
  • 我的symfony版本是2.6.5
  • 这个问题被标记为 symfony 2.3 误导了我。您的请求对象可能存在问题。你检查过你的请求对象吗?根据这个答案,stackoverflow.com/a/20985212/3399234 getRequest() 将被弃用。您可以尝试通过这种方式将您的请求传递给您的控制器,公共函数 showCategoryAction(Request $request, $id, $page)
猜你喜欢
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 2013-06-22
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多