Symfony dependent dropdown with 3 entities

删除回忆录丶 提交于 2019-12-08 04:28:33

问题


So I'm about to create a form with three dropdowns which are interdependent. When one or several channel1s are selected, the choices for channel 3 should change, according to what is selected for channel1. And dependent on that, the last dropdown "agencies" should change its choices. I've already tried different solutions but none of them has worked so far. Right now I'm stuck on the solution provided by Symfony's documentation, which provides code for two entities but even with that one, my second dropdown doesn't have any values, so it's not working. Here is my Form Type:

class SelectionType extends AbstractType {

  protected $tokenStorage;
  // private $manager;
  public function __construct(TokenStorageInterface $tokenStorage)
  {
      $this->tokenStorage = $tokenStorage;


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

      //solution Symfony Documentation

    $channel1s = new Channel1();
      $currentuser = $this->tokenStorage->getToken()->getUser();
      $builder
      ->add('channel1s', EntityType::class, array(
        'class' => 'AppBundle:Channel1',
        'property' => 'name',
        'label' => 'label.channel1s',
        'empty_value' => 'label.select_channel1s',
        'mapped' => false,
        'expanded' => false,
        'translation_domain' => 'UploadProfile',
        'multiple' => true,
        'required' => false,

      ));
      $formModifier = function (FormInterface $form, Channel1 $channel1s = null) {
        $channel3s = null === $channel1s ? array() : $channel1s->getChannel3();
        $form->add('channel3s', EntityType::class, array(
          'class' => 'AppBundle:Channel3',
          'property' => 'name',
          'label' => 'label.channel3s',
          'empty_value' => 'label.select_channel3s',
          'mapped' => false,
          'expanded' => false,
          'translation_domain' => 'UploadProfile',
          'choices' => $channel3s,
          'multiple' => true,
          'choices_as_values' => true,
        ));
      };

      $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($formModifier) {
          $data = $event->getData();

          $formModifier($event->getForm(), $data->getChannel1s());
        }
      );
        $builder->get('channel1s')->addEventListener(
          FormEvents::POST_SUBMIT,
          function (FormEvent $event) use ($formModifier) {
            $channel1s = $event->getForm()->getData();
            $formModifier($event->getForm()->getparent(), $channel1s);
          }
        );
      }

  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
        $resolver->setDefaults(array(
            'data_class' => 'DocumentBundle\Entity\UploadProfile'
        ));
  }


  public function getName()
  {
      return 'uploadprofile';
  }


}

I've also tried a solution with Subscribers from that page: http://showmethecode.es/php/symfony/symfony2-4-dependent-forms/ but it didn't work out either.. I think my problem is somewhere around that line:

$channel3s = null === $channel1s ? array() : $channel1s->getChannel3();

but that's just a guess.. I also added that ajax function:

var $channel1s = $('#uploadprofile_channel1s');
$channel1s.change(function() {
  var $form = $(this).closest('form');
  var data = {};
  data[$channel1s.attr('name')] = $channel1s.val();
  // data[channel3s.attr('name')] = channel3s.val();
  $.ajax({
    url : $form.attr('action'),
    type: $form.attr('method'),
    data : data,
    success: function(html) {
      $('#uploadprofile_channel3s').replaceWith(
        $(html).find('#uploadprofile_channel3s')
      );
    }
  });
});

My 3 entities have ManytoMany or OneToMany relationships and I should have all the right getters and setters, but if anyone needs them to varify, let me know and I will upload them!

I've been stuck on this for quite a while now, so I would be happy about any kind of help or advise!

NOTE: there's still a third entity (agency) to be added but since not even the first one's are working, I decided to upload only the first two..

ADDED: or maybe somebody can explain to my that line:

$channel3s = null === $channel1s ? array() : $channel1s->getChannel3s();
might be, that this is my problem?

来源:https://stackoverflow.com/questions/45758982/symfony-dependent-dropdown-with-3-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!