Jump to content
Sign in to follow this  
davida7

Symfony dynamic forms (select change)

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.