How to get data from a form who using method GET

118 Views Asked by At

I have a problem with my form. I tried to get the value from form but no result. My form is:

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

My controller :

$entity = new Product();
    $form = $this->createForm(new ProductType(), $entity);
    $request = $this->getRequest();
    $form->handleRequest($request);

    //Get filter array from search
    if ($form->isValid()) {
        $aFilter['iMinPrice'] = $form["min_price"]->getData();
        $aFilter['iMaxPrice'] = $form["max_price"]->getData();
    }
    print_r($aFilter);

My ProductRepository:

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('min_price', 'text', array('mapped' => false, 'label' => 'From :', 'attr'=>
                                       array(
                                            'placeholder'=>'Max price',
                                            'class'=>'form-control')))

            ->add('max_price', 'text', array('mapped' => false, 'label' => 'To :' , 'attr'=>
                                        array(
                                            'placeholder'=>'Minim price',
                                            'class'=>'form-control')))

            //->add('colors', 'choice', array('mapped' => false, 'choices' => Colors::getColors(), 'multiple' => TRUE, 'expanded' => TRUE))
    ;
}

aFilter is NULL but if I using POST method in aFilter I get the value from form. Please help me!

1

There are 1 best solutions below

0
Dan Blows On

The method in the form configuration needs to match the method of the HTTP request. By default, forms use the POST method, so you need to tell it to use GET.

Put this in the buildForm() method:

$builder->setMethod('GET');

Then in the Twig template, you can use the form_start() function and it will automatically set the method to be GET.