2017-02-14 191 views
0

經過多次搜索和嘗試,我不知道如何解決我的問題。Symfony2集合的選擇每個元素的不同選項

我發現這個問題:Symfony2 Form Collection Field with Different Choices但解決方案沒有給予搜索線索,我沒有找到如何適應我的情況。

我與本地化,地區之間有多對多的關係,本地化與城市之間的關係與部門之間的多對多關係以及多對多的關係。

要創建一個定位,我有這種形式:

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' => ''), 
    )); 

} 

我選擇ChoiceType空的,因爲我有幾個市爲例(更25K),所以我喜歡用AJAX他們的負荷少在我看來,和渲染它們在一個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> 

更改標籤,如果我知道我需要創建自己的模板,確定,但只顯示選項選擇,而不是其他人我想我需要在PRE_SET_DATA FormEventListener,但我看不到如何實現這一點。所以如果有人有解決方案,我會接受。

回答

0

我試着用PRE_SET_DATA上的FormEventListener,POST_SET_DATA或者choice_loader嘗試用Symfony方法做我想做的事,但我不明白爲什麼,我回答了我自己的問題。我感覺又回到jQuery的方法來消除誰不與下面的代碼,我不喜歡所選的選項,但我沒有看到其他的解決方案:

<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> 

我不及格的話題,因爲在案件告破我錯了