php - Retrieve data from a form with GET method using symfony2 -



php - Retrieve data from a form with GET method using symfony2 -

i can't retrieve info form, tried differents ways no result. repository :

public function buildform(formbuilderinterface $builder, array $options) { $builder->add('min_price', 'text', array('mapped' => false, 'label' => 'de la :', 'attr'=> array( 'placeholder'=>'pretul minim', 'class'=>'form-control'))) ->add('max_price', 'text', array('mapped' => false, 'label' => 'pina la :' , 'attr'=> array( 'placeholder'=>'pretul maxim', 'class'=>'form-control'))) ) ; } public function setdefaultoptions(optionsresolverinterface $resolver) { parent::setdefaultoptions($resolver); $resolver->setdefaults(array( // avoid pass csrf token in url (but it's not protected anymore) 'csrf_protection' => false, )); } public function getname() { homecoming ''; }

my controller :

public function showcategoryaction($id, $page, request $request){ $em = $this->getdoctrine()->getmanager(); $repositoryproduct = $em->getrepository('shopdesktopbundle:product'); $category = $em->getrepository('shopdesktopbundle:category')->findonebyid($id); if (!$category) { throw $this->createnotfoundexception('category not found.'); } $afilter = array(); $entity = new product(); $form = $this->createform(new producttype(), $entity,array( 'action' => $this->generateurl('show_product_category',array("id" => $id, "name" => $category->getcategorylink(), "page" => $page )), //your url generate 'method' => 'get' )); $form->handlerequest($request); $afilter['iminprice'] = $form["min_price"]->getdata(); $afilter['imaxprice'] = $form["max_price"]->getdata(); print_r($afilter); //searchs products $aproducts = $repositoryproduct->getproductsorderbydatedesc($id,null,$afilter); if (!$aproducts) { throw $this->createnotfoundexception('products not found.'); } //create pagination $paginator = $this->get('knp_paginator'); $pagination = $paginator->paginate( $aproducts, $page, 3 ); //send info view homecoming $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) }}> <div class="accordion-group"> <div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="" href="#toggleone"> <em class="icon-minus icon-fixed-width"></em>pret </a> </div> <div id="toggleone" class="accordion-body collapse in"> <div class="accordion-inner"> {{ form_widget(form) }} </div> </div> </div> <input type="submit" class="btn btn-primary marg-left-20" value="cautare"/> </form>

the view :

show_product_category: path: /{id}/{name}/{page} defaults: { _controller: shopdesktopbundle:category:showcategory, page: 1} requirements: id: \d+ page: \d+ _method: get|post

so problem can't retrieve info thi form. situations $afilter empty. if illustration set in view in form post method filter info form. url : ?min_price=10&max_price=50. help me please. thx in advance.exist solution??

i not set form method get, not specify method , default post, usual way submit form.

then handle form submission in controller when method post - this:

if ($request->ismethod('post')) { $form->handlerequest($request); if ($form->isvalid() { $afilter['iminprice'] = $form->get('min_price')->getdata(); $afilter['imaxprice'] = $form->get('max_price')->getdata(); } }

more info on how handle form submissions in symfony2 here.

if need form method get, should able query string parameters request:

$afilter['iminprice'] = $request->query->get('min_price'); $afilter['imaxprice'] = $request->query->get('max_price');

php symfony2 symfony-forms php-5.3 symfony-2.3

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -