2017-01-18 5 views
-1

だから私はアイテムを並べ替えるだろう、このChoiceType Form持っている:$products = "SELECT a FROM AppBundle:Product a ORDER BY a.title ASC"ハンドル要求:ChoicesType

$sort = $this->createForm(ChoiceType::class, NULL, array(
    'choices' => array(
     '...' => 'default', 
     'A-Z' => 'title_up', 
     'Z-A' => 'title_down', 
     'Price low to high' => 'price_up', 
     'Price high to low' => 'price_down', 
    ), 
)); 

を、私はそれらの一つは、ドロップダウンメニューから選択されたときにようにChoicesを使用するには、これを行います。

私はこれを試してみました:定義されていないので、

$sort->handleRequest($request); 
if($sort->isSubmitted() && $sort->isValid()) { 
    if (isset($default)) { 
     $products = "SELECT a FROM AppBundle:Product a ORDER BY a.title ASC"; 
     return $this->render('AppBundle:main:index.html.twig', array('products' => $products,)); 
    } 
} 

しかし$defaultは、機能していません。私はchoicesにアクセスする方法を知らないので、ifステートメントにそれらを渡すことができます。

私はあなたがこのような何か書く必要があると思います

答えて

0

:値は、あなたが期待するもので、その後、作成した場合

$select = $request->request->get('select'); // this will contain whatever value you've selected from the dropdown 

チェック:<select>要素内の値を取得するには

$sort = $this->createFormBuilder() 
    ->setAction($this->generateUrl('your_process_route_here')) 
    ->setMethod('POST') 
    ->add('select', ChoiceType::class, [ 
     'placeholder' => 'Please select', 
     'choices' => [ 
      '...' => 'default', 
      'A-Z' => 'title_up', 
      'Z-A' => 'title_down', 
      'Price low to high' => 'price_up', 
      'Price high to low' => 'price_down', 
     ] 
    ]) 

をクエリ:から

if ('default' == $select){ // or you can use a switch 
    // create a custom method inside your Repository class containing the SELECT, and call it here 
} 

selectこと3230は、<select> html要素の名前属性になります。