dynamic - Symfony dependent dropdown with 3 entities -
so i'm create form 3 dropdowns interdependent. when 1 or several channel1s selected, choices channel 3 should change, according selected channel1. , dependent on that, last dropdown "agencies" should change choices. i've tried different solutions none of them has worked far. right i'm stuck on solution provided symfony's documentation, provides code 2 entities one, second dropdown doesn't have values, it's not working. here 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 tried solution subscribers page: http://showmethecode.es/php/symfony/symfony2-4-dependent-forms/ didn't work out either.. think problem somewhere around line:
$channel3s = null === $channel1s ? array() : $channel1s->getchannel3();
but that's guess.. added 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 , should have right getters , setters, if needs them varify, let me know , upload them!
i've been stuck on quite while now, happy kind of or advise!
note: there's still third entity (agency) added since not first one's working, decided upload first two..
added: or maybe can explain line:
$channel3s = null === $channel1s ? array() : $channel1s->getchannel3s(); might be, problem?
Comments
Post a Comment