How to get data from a form in symfony2

喜夏-厌秋 提交于 2019-12-24 23:18:46

问题


I have a method :

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

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>

I search and normally everything is ok but my $var variable is null. I don't understand where is my problem, probably I missing something. It's a good idea to create forms who is not mapped in controller?. Please help me. Thx in advance


回答1:


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

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

I am not shore but this should work for you




回答2:


If you are using Symfony 2.3 you can do it this way:

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

Also, in case I doesn't work or the $request is empty, you can also get the $request in another way:

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


来源:https://stackoverflow.com/questions/29485240/how-to-get-data-from-a-form-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!