2017-02-14 75 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> 

我不及格的话题,因为在案件告破我错了