2016-08-11 7 views
1

私はSynfony3を使用しています。私は「カテゴリ」「サービス」「プロファイル」と呼ばれる3つのエンティティを持ち、その関係はC < 1-n> S < 1-n> Pです。他の言葉では、1つのカテゴリは複数のサービスを持つことができ、私はそうするために、すべてのカテゴリsymfonyカスケードドロップダウンリスト

ですべてのサービスと1つのドロップダウンリストで1つのドロップダウンを持っているために、私は希望のプロファイルを水和するためのフォームを表示する場合

を次のようにプロファイルが。

、私はprofileTypeがでサービスのドロップダウンリストを入れています

class ProfileType extends AbstractType { 
     //... 
     ->add('service',  EntityType::class, array(
      'class'  => 'xxxBundle:Service', 
      'choice_label' => 'name', 
      'multiple'  => false, 
     )) 

カテゴリを追加するには、カテゴリとPrの間に直接の接続がないあなたの助けを事前に

おかげOFILE

+1

このようなものをお探しですか? https://symfony.com/doc/current/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms – dbrumann

答えて

0

することはでき適応エンティティ(...選択肢をロードするために分離サービスでリスナーを入れてEntityMangerを注入)を扱うことができるように提供されている例:

これは、カテゴリ選択フィールドの値に応じてサブカテゴリを選択し、フィールド上許容される選択肢を変更する方法を示すための一例であるどのように選択フィールド

をカスケード接続する

。 これを行うには、クライアント側とサーバー側の両方でサブカテゴリの選択を動的にする必要があります。

  1. (Javascriptを/ jQueryのを使用して)クライアント側の動的フォームの表示 例えば、クライアント側のフォームが動的にする:あなたはAJAX呼び出しから選択肢を得ることができる。もちろん、

    $('#category').change(function(){ 
        switch($(this).val()){ 
         case '1': // If category == '1' 
          var choice = { 
           'choice1_1':'1_1', 
           'choice1_2':'1_2', 
           'choice1_3':'1_3', 
          }; 
         break; 
         case '2': // If category == '2' 
          var choice = { 
           'choice2_1':'2_1', 
           'choice2_2':'2_2', 
           'choice2_3':'2_3', 
          }; 
         break; 
         case '3': // If category == '3' 
          var choice = { 
           'choice3_1':'3_1', 
           'choice3_2':'3_2', 
           'choice3_3':'3_3', 
          };   
         break; 
        } 
    
        var $subCategorySelect = $('#subCategory'); 
    
        $subCategorySelect.empty(); 
        $.each(choice, function(key, value) { 
         $subCategorySelect.append($('<option></option>')).attr('value',value).text(key); 
        }); 
    }); 
    

    。これはこの例の目的ではありません。

  2. は、サーバー側の動的フォームの初期化と検証 例のためのサーバ側のフォームを動的にする:アルザスとDbrumannへの応答に基づいて

    namespace AppBundle\Form; 
    
    use Symfony\Component\Form\AbstractType; 
    use Symfony\Component\Form\FormBuilderInterface; 
    
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 
    
    use Symfony\Component\Form\FormEvent; 
    use Symfony\Component\Form\FormEvents; 
    
    class MyBaseFormType extends AbstractType 
    { 
        /** 
        * @param FormBuilderInterface $builder 
        * @param array $options 
        */ 
        public function buildForm(FormBuilderInterface $builder, array $options) 
        { 
         $builder 
          ->add('category',ChoiceType::class,array('choices'=>array(
            'choice1'=>'1', 
            'choice2'=>'2', 
            'choice3'=>'3', 
           ))) 
         ; 
    
         $addSubCategoryListener = function(FormEvent $event){ 
          $form = $event->getForm(); 
          $data = $event->getData(); 
    
          switch($data['category']){ 
           case '1': // If category == '1' 
            $choices = array(
             'choice1_1'=>'1_1', 
             'choice1_2'=>'1_2', 
             'choice1_3'=>'1_3', 
            ); 
           break; 
           case '2': // If category == '2' 
            $choices = array(
             'choice2_1'=>'2_1', 
             'choice2_2'=>'2_2', 
             'choice2_3'=>'2_3', 
            );       
           break; 
           case '3': // If category == '3' 
            $choices = array(
             'choice3_1'=>'3_1', 
             'choice3_2'=>'3_2', 
             'choice3_3'=>'3_3', 
            );       
           break; 
          } 
    
          $form->add('subCategory',ChoiceType::class,array('choices'=>$choices)); 
         }; 
    
         // This listener will adapt the form with the data passed to the form during construction : 
         $builder->addEventListener(FormEvents::PRE_SET_DATA, $addSubCategoryListener); 
    
         // This listener will adapt the form with the submitted data : 
         $builder->addEventListener(FormEvents::PRE_SUBMIT, $addSubCategoryListener); 
        } 
    } 
    
関連する問題