davida7 Posted December 27, 2016 Share Posted December 27, 2016 Hello all, I have been developing with Symfony and ran into the problem of creating dynamic forms. My intention is to change part of a form by a select. Basically making a part of a form dependent on a select value. I have followed the docs from Symfony, but can't seem to understand it: http://symfony.com/doc/current/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms This is an example I am working on: http://stackoverflow.com/questions/41281486/symfony-alter-form-on-entity-select-with-nested-collectiontypes/41283649#41283649 So far no one could really help. Based on that stackoverflow post, this is desired: 1. Select a company 2. Generate the forms for the departments of the selected company Some how when I try to work in PRE_SUBMIT or the form modifier function I can't get any posted values. How could I achieve the example from the stackoverflow post? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/302818-symfony-dynamic-forms-select-change/ Share on other sites More sharing options...
kicken Posted December 29, 2016 Share Posted December 29, 2016 (edited) How is your controller processing your ajax request? What is the code you currently have to build your form? Your PRE_SUBMIT handler is passed an event object and you can get the posted data through that. You can use that to modify your form as needed. For example, I have a form that does: $builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event){ $this->updateTypeConfigurationForm($event); }); The updateTypeConfigurationForm method modifies the form based on the selected type which is a select field in the form. private function updateTypeConfigurationForm(FormEvent $event){ $data = $event->getData(); $type = null; if (isset($data['answerType'])){ $type = $data['answerType']; } $form = $event->getForm(); $form->add('typeConfiguration', new SurveyAnswerTypeConfigurationForm(), [ 'required' => false , 'label' => 'Type Configuration' , 'error_bubbling' => true , 'type' => $type ]); } Edited December 29, 2016 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/302818-symfony-dynamic-forms-select-change/#findComment-1540839 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.