2017-02-14 2 views
0

Nach mehreren suchen und versuchen, weiß ich nicht, wie ich mein Problem zu lösen.Symfony2 Sammlung von Optionen verschiedene Option für jedes Element

Ich fand dies: Symfony2 Form Collection Field with Different Choices aber Lösung wurde nicht nur einen Suchpfad gegeben und ich habe nicht gefunden, wie Lösung in meinem Fall anzupassen.

Ich habe viele zu viele Beziehung zwischen Lokalisierung und Region, viele zu viele Beziehung zwischen LOcalization und Abteilung und viele zu viele Beziehung zwischen Lokalisierung und Stadt.

Um eine Lokalisierung zu erstellen Ich habe dieses Formular:

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

} 

ich eine Choice wählen leer, weil ich zum Beispiel mehrere Stadt habe (mehr von 25k) so dass ich lieber Last wenige von ihnen mit AJAX in meiner Sicht und render sie in einem select2, es funktioniert für add aktion aber für edit action Ich habe ein problem Ich möchte jedes Feld meiner Sammlungen einen anderen Wert haben.

meine Geschichte verdeutlichen, möchte ich dieses Ergebnis:

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

Und das Ergebnis, das ich im Moment haben, ist: Ich muss meine eigene Gruppe gründen

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

Etikett zu ändern, wenn ich verstehe, Vorlage, ok, aber nur Option anzeigen ausgewählt und nicht andere Ich denke, ich brauche einen FormEventListener auf PRE_SET_DATA, aber ich sehe nicht, wie dies implementieren. Wenn jemand eine Lösung hat, nehme ich es.

Antwort

0

Ich beantworte meine eigene Frage, nach dem Versuch mit FormEventListener auf PRE_SET_DATA, auf POST_SET_DATA, oder mit einem choice_loader scheint es unmöglich zu sein, was ich will mit einem Symfony Weg, aber ich verstehe nicht warum. Ich fühle mich auf JQuery Weg zurück Option zu entfernen, die mit folgendem Code nicht ausgewählt sind, ich mag nicht, aber ich sehe nicht, andere Lösungen:

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

Ich habe nicht das Thema passieren wie im Fall gelöst, ich bin falsch

Verwandte Themen