Sirius
Sirius

Reputation: 653

Search filter with method get doesn't found results [symfony2]

I have a search form that works with the method POST, but the method POST doesn't display the requested data in the url.

With method POST the url look like this:

/search_flight

with the method GET no results found, the url look like this:

/search_flight?from=Cape+Town%2C+International+CPT&to=Johannesburg%2C+O.R.+Tambo+International+JNB&departuredate=2016%2F01%2F08&arrivaldate=2016%2F10%2F04&price=57.5%2C1000

I also noticed that with the method GET the data is reset in each input of the form.

routing.yml

searchFlight:
    path: /search_flight
    defaults:  {  _controller: FLYBookingsBundle:Post:searchtabflightResult }
    requirements:
        _method: GET|POST

controller

This method send the requested data to the method searchtabflightResultAction that will handle the query.

public function searchtabflightAction()
{
    //$form = $this->createForm(new SearchflightType(),null, array('action' => $this->generateUrl('searchFlight'),'method' => 'GET',));
    $form = $this->get('form.factory')->createNamed(null, new SearchflightType());
    return $this->render('FLYBookingsBundle:Post:searchtabflight.html.twig', array(
        'form' => $form->createView(),
    ));
}

.

<form action="{{ path ('searchFlight') }}" method="GET">
{# here I have my forms #}
</form>

.

public function searchtabflightResultAction(Request $request)
{
    //$form = $this->createForm(new SearchflightType());
    $form = $this->get('form.factory')->createNamed(null, new SearchflightType());
    $form->handleRequest($request);
    $em = $this->getDoctrine()->getManager();

    $airport1 = $form["to"]->getData();
    $airport = $form["from"]->getData();
    $departureDateObj = $form["departuredate"]->getData();
    $arrivalDateObj = $form["arrivaldate"]->getData();
    $price = $form["price"]->getData();

    $entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($airport1,$airport,$departureDateObj,$arrivalDateObj,$price);


    return $this->render('FLYBookingsBundle:Post:searchtabflightResult.html.twig', array(
        'entities' => $entities,
        'form' => $form->createView(),
    ));
}

How can I make my search filter works with method get ?

Upvotes: 0

Views: 498

Answers (1)

Jan Rydrych
Jan Rydrych

Reputation: 2258

Everything should be done within two actions, the basic concept is:

SearchFlightType has with/wo price option:

class SearchFlightType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('from', FormType\TextType::class)
            ->add('to', FormType\TextType::class)
            ->add('departuredate', FormType\TextType::class)
            ->add('arrivaldate', FormType\TextType::class);
        if ($options['price']) {
            $builder->add( 'price', FormType\TextType::class );
        }
        $builder
            ->add('submit', FormType\SubmitType::class);
    } 

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'price' => false,
        ));
    }
}

Controller.php

class PostController extends Controller
{
    /**
     * @Route("/index", name="index")
     */
    public function indexAction(Request $request)
    {
        $defaultData = array();
        $form = $this->createForm(SearchFlightType::class, $defaultData, array(
            // action is set to the specific route, so the form will
            // redirect it's submission there
            'action' => $this->generateUrl('search_flight_result'),
            // method is set to desired GET, so the data will be send
            //via URL params
            'method' => 'GET',
        ));

        return $this->render('Post/searchtabflight.html.twig', array(
            'form' => $form->createView(),
        ));
    }

    /**
     * @Route("/search_flight_result", name="search_flight_result")
     */
    public function searchTabFlightResultAction(Request $request)
    {
        $defaultData = array();
        $entities = null;
        $form = $this->createForm(SearchFlightType::class, $defaultData, array(
            // again GET method for data via URL params
            'method' => 'GET',
            // option for price form field present
            'price' => true,
        ));

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // get data from form
            $data = $form->getData();

            // process the data and get result
            $em = $this->getDoctrine()->getManager();
            $entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($data['from'], $data['to'], ...);
        }

        return $this->render('Post/searchtabflight.html.twig', array(
            'form' => $form->createView(),
            // present the result
            'entities' => $entites,
        ));
    }
}

Upvotes: 1

Related Questions