使用方法获取搜索过滤器没有找到结果[symfony2]

时间:2017-02-11 11:38:34

标签: php symfony twig

我有一个与方法POST一起使用的搜索表单,但方法POST不会在网址中显示请求的数据。

使用方法POST,网址如下所示:

/search_flight

方法GET未找到任何结果,网址如下所示:

/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

我还注意到,使用方法GET,数据会在表单的每个输入中重置。

的routing.yml

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

控制器

此方法将请求的数据发送到将处理查询的方法searchtabflightResultAction

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

如何使我的搜索过滤器与方法get?

一起使用

1 个答案:

答案 0 :(得分:1)

一切都应该在两个行动中完成,基本概念是:

SearchFlightType具有/ wo价格选项:

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