2

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!

3
  • $form->get('min_price')->getData() ? Commented Apr 1, 2015 at 12:37
  • You created your form with $entity as value, so your form values are mapped in the $entity variable. Just call your product methods : $entity->getMinPrice(); $entity->getMaxPrice(); Commented Apr 1, 2015 at 12:47
  • I edited the question. The min and max price not part of the entity. I repeat for method POST this works but for GET doesn't work Commented Apr 1, 2015 at 14:44

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.