2017-02-14 9 views
0

いくつかの検索と試行の後、私は自分の問題を解決する方法を知らない。Symfony2各要素の異なる選択肢のコレクション

私はこれを見つけました:Symfony2 Form Collection Field with Different Choicesしかし、解決策は検索トレイルしか与えられず、私の場合は解決策を見つける方法が見つかりませんでした。

ローカリゼーションと地域との間には多対多の関係がありますが、ローカライゼーションと市区町村のLOcalizationとDepartmentとMany-to-Manyリレーションシップの多対多の関係があります。私はこのフォームを持っているローカライズ作成するには

:私は、たとえば、いくつかの都市を持っているので、私は(25Kのそれ以上)の空ChoiceTypeを選択するので、私は私の見解でAJAXとそれらの負荷数を好む

class LocalizationType extends AbstractType{ 


private $manager; 

public function __construct(ObjectManager $manager) 
{ 
    $this->manager = $manager; 
} 

public function buildForm(FormBuilderInterface $builder, array $options){ 


    $builder->add('regions', CollectionType::class, array('entry_type' => ChoiceType::class, 
                  'allow_add' => true, 
                  'allow_delete' => true, 
                  'required' => false, 
                  'entry_options' => array(
                   'choices' => (array_key_exists('regions', $options['localization_value']) ? $options['localization_value']['regions'] : array('' => '')), 
                   'multiple' => false, 
                   'expanded' => false, 
                   'attr' => array('class' => 'region input'), 

                   ), 
                  'data' => (array_key_exists('regions', $options['localization_data']) ? $options['localization_data']['regions'] : null), 
                  )) 
      ->add('departments', CollectionType::class, array('entry_type' => ChoiceType::class, 
                   'allow_add' => true, 
                   'allow_delete' => true, 
                   'required' => false, 
                   'entry_options' => array(
                   'choices' => (array_key_exists('departments', $options['localization_value']) ? $options['localization_value']['departments'] : array('' => '')), 
                    'multiple' => false, 
                    'expanded' => false, 
                    'attr' => array('class' => 'department input') 
                    ), 
                   'data' => (array_key_exists('departments', $options['localization_data']) ? $options['localization_data']['departments'] : null), 
                   )) 
      ->add('cities', CollectionType::class, array('entry_type' => ChoiceType::class, 
                  'allow_add' => true, 
                  'allow_delete' => true, 
                  'required' => false, 
                  'entry_options' => array(
                   'choices' => (array_key_exists('cities', $options['localization_value']) ? $options['localization_value']['regions'] : array('' => '')), 
                   'multiple' => false, 
                   'expanded' => false, 
                   'attr' => array('class' => 'city input') 
                   ), 
                  'data' => (array_key_exists('cities', $options['localization_data']) ? $options['localization_data']['cities'] : null), 
                  )) 
    ; 

    $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event){ 

     $data = $event->getData(); 

     if(!empty($data['regions']) && is_array($data['regions'])){ 

      $regions = array(); 

      foreach($data['regions'] as $region){ 

       $regions[] = $region; 

      } 

      $data['regions'] = $this->manager->getRepository('LocalizationBundle:Region')->findRegionsForCreateEntity($regions); 

     } 

     if(!empty($data['departments']) && is_array($data['departments'])){ 

      $departments = array(); 

      foreach($data['departments'] as $department){ 

       $departments[] = $department; 

      } 

      $data['departments'] = $this->manager->getRepository('LocalizationBundle:Departments')->findDepartmentsForCreateEntity($departments); 

     } 

     if(!empty($data['cities']) && is_array($data['cities'])){ 

      $cities = array(); 

      foreach($data['cities'] as $city){ 

       $cities[] = $city; 

      } 

      $data['cities'] = $this->manager->getRepository('LocalizationBundle:City')->findCitiesForCreateEntity($cities); 

     } 

     $form = $event->getForm(); 

     $form->add('regions', CollectionType::class, array('entry_type' => ChoiceType::class, 
                  'allow_add' => true, 
                  'allow_delete' => true, 
                  'required' => false, 
                  'entry_options' => array(
                   'choices' => $data['regions'], 
                   'multiple' => false, 
                   'expanded' => false, 
                   'attr' => array('class' => 'region input') 
                   ) 
                  )); 


       $form->add('departments', CollectionType::class, array('entry_type' => ChoiceType::class, 
                    'allow_add' => true, 
                    'allow_delete' => true, 
                    'required' => false, 
                    'entry_options' => array(
                     'choices' => $data['departments'], 
                     'multiple' => false, 
                     'expanded' => false, 
                     'attr' => array('class' => 'department input') 
                     ) 
                    )) 
       ->add('cities', CollectionType::class, array('entry_type' => ChoiceType::class, 
                   'allow_add' => true, 
                   'allow_delete' => true, 
                   'required' => false, 
                   'entry_options' => array(
                    'choices' => $data['cities'], 
                    'multiple' => false, 
                    'expanded' => false, 
                    'attr' => array('class' => 'city input') 
                    ) 
                   )) 
    ; 


    }); 

} 

public function configureOptions(OptionsResolver $resolver){ 

    $resolver->setDefaults(array(
     'data_class' => Localization::class, 
     'localization_data' => array('regions' => '', 'departments' => '', 'cities' => ''), 
     'localization_value' => array('regions' => '', 'departments' => '', 'cities' => ''), 
    )); 

} 

をし、それらをselect2でレンダリングすると、アクションを追加するために機能しますが、編集アクションでは、私のコレクションの各フィールドに異なる値を設定したいという問題があります。

<label>Region n°1</label> 
<select id="" name=""> 
    <option value="foo1">bar1</option> 
</select> 

<label>Region n°2</label> 
<select id="" name=""> 
    <option value="foo2">bar2</option> 
</select> 

そして、私は一瞬のために持っている結果である:私は、私は自分自身を作成する必要があります理解していれば

<label>0</label> 
<select id="" name=""> 
    <option value="foo1" selected="selected">bar1</option> 
    <option value="foo2">bar2</option> 
</select> 

<label>1</label> 
<select id="" name=""> 
    <option value="foo1">bar1</option> 
    <option value="foo2" selected="selected">bar2</option> 
</select> 

は、ラベルを変更する

私の話を説明するために、私はこの結果をしたいですテンプレートはOKですが、選択されたオプションだけを表示するようにしました。他のものは表示しません。PRE_SET_DATAにFormEventListenerが必要だと思いますが、実装方法はわかりません。だから誰かが解決策を持っているなら、私はそれを取るでしょう。

答えて

0

FormEventListenerをPRE_SET_DATA、POST_SET_DATA、またはchoice_loaderで試した後、私自身の質問に答えていますが、Symfonyの方法ではできないように思えますが、理由はよくわかりません。場合に解決として、私は、トピックを渡さない私が間違っている

<script type="text/javascript"> 

    $.each($('.region'), function(i, val){ 

     $(val).find('option:not(:selected)').remove(); 

    }) 

    $.each($('.department'), function(i, val){ 

     $(val).find('option:not(:selected)').remove(); 

    }) 

    $.each($('.city'), function(i, val){ 

     $(val).find('option:not(:selected)').remove(); 

    }) 
</script> 

:私は、コードを次のように選択されていないオプションを削除するために戻ってjQueryの途中に感じる私が嫌いが、私は他のソリューションが表示されません

関連する問題