Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:25:10+00:00 2026-06-06T06:25:10+00:00

In a Symfony2 website I’m trying to make a form with 2 (or 3)

  • 0

In a Symfony2 website I’m trying to make a form with 2 (or 3) dropdown lists with a dependency like Country > Region > City. And that city is a field of the element I’m editing with the form. The idea is to fill the lists depending on selections.

I’ve followed the tutorial with form events here : http://aulatic.16mb.com/wordpress/2011/08/symfony2-dynamic-forms-an-event-driven-approach/
(which is based on webb-on-the-web .com/?p=5)

The issue I have: it all works but when I use the form to edit the element, the city is selected correctly (from DB) but the Country and Region dropdown lists are prefilled and left on ‘select a value’. I don’t know if it was supposed to work with the tutorial as it is.

The question : how can I make these lists selected? I’m trying to add a POST_SET_DATA event but I can’t find a way to select the value in the form field.

Here’s the form class : http://pastebin.com/PpWkHxC3 (note that instead of city it’s : Field > Topic and topic is a field of a Lesson which the form edits).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:25:11+00:00Added an answer on June 6, 2026 at 6:25 am

    I almost had it. If anybody else ever needs this here’s what needs to be added to make this solution work perfectly when editing an existing item :

    class ItemDetailForm extends AbstractType
    {
       ...
            $builder->addEventListener(FormEvents::POST_SET_DATA, function (DataEvent $event) use ($refreshTopic) {
                $data = $event->getData();
                $form = $event->getForm();
                if (null === $data) {
                    return;
                }
    
                $form->get('region')->setData($data->getCity()->getRegion());
            });
    }
    

    Edit: since symfony 2.1, the POST_SET_DATA event is called before the children are added to the form, causing all the get(‘region’) to raise an exception.
    The solution is to create this field in the POST_SET_DATA and not in the buildForm() :

            /** @var FormFactory $factory */
            $form->add($factory->createNamed('region', 'entity', null, array(
                'class'=>'AcmeBundle:Region',
                'property_path'=>false,
                'empty_value'=>'Choose a value',
                'required'=>true,
                'label'=>'Region'
            )));
    

    Note that you need to add the $factory to the ‘use’ of the closure handling the event :

    $builder->addEventListener(FormEvents::POST_SET_DATA, function (DataEvent $event) use ($refreshTopic, $factory) {
    

    Here is the whole form class:

    <?php
    namespace AAA\CoreBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\FormFactory;
    use Symfony\Component\Form\FormEvents;
    use Symfony\Component\Form\FormEvent;
    use Symfony\Component\Form\Form;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Doctrine\ORM\EntityRepository;
    use AAA\CoreBundle\Entity\ClassYear;
    use AAA\CoreBundle\Entity\Field;
    use AAA\CoreBundle\Entity\Lesson;
    use AAA\CoreBundle\Form\LessonContentForm;
    
    class LessonDetailForm extends AbstractType
    {
        public $country;
        function __construct($country=null) {
            // Get country for classyear dropdown list
            $this->country = $country;
        }
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $factory = $builder->getFormFactory();
    
            $builder->add('name', null, array('label'=>'Titre de la leçon'));
            $builder->add('description', 'textarea', array('label'=>'Description (définition conceptuelle) Qu\'est-ce que c\'est ? Et à quoi ça sert ? (importance, utilité)'));
            $builder->add('text', 'textarea', array('label'=>'Leçon', 'required'=>false)); // Can't set 'required' on textareas used by TinyMCE
            $builder->add('reperes', 'textarea', array('label'=>'Repères (détectionel) - Quels sont les éléments qui me permettent de repérer que je dois penser à ce concept ?', 'required'=>false));
            $builder->add('other_topic', null, array(
                'required'  =>  false,
                'mapped'     =>  false
            ));
    
            $refreshField = function ($form, $classyear) use ($factory) {
                /** @var FormFactory $factory */
                /** @var Form $form */
                $form->add($factory->createNamed('field','entity',null, array(
                    'class'         => 'AAA\CoreBundle\Entity\Field',
                    'mapped'        => false,
                    'label'         => 'Matière',
                    'empty_value'   => 'Sélectionne une valeur',
                    'empty_data'    => null,
                    'required'      => false,
                    'query_builder' => function (EntityRepository $repository) use ($classyear) {
                        $qb = $repository->createQueryBuilder('field')
                            ->innerJoin('field.classyear', 'classyear');
    
                        if($classyear instanceof ClassYear) {
                            $qb = $qb->where('field.classyear = :classyear')
                                ->setParameter('classyear', $classyear);
                        } elseif(is_numeric($classyear)) {
                            $qb = $qb->where('classyear.id = :classyear_id')
                                ->setParameter('classyear_id', $classyear);
                        } else {
                            $qb = $qb->where('0 = 1');
                        }
    
                        return $qb;
                    }
                )));
            };
            $refreshTopic = function ($form, $field) use ($factory) {
                /** @var FormFactory $factory */
                /** @var Form $form */
                $form->add($factory->createNamed('topic','entity',null, array(
                    'class'         => 'AAA\CoreBundle\Entity\Topic',
                    'property'      => 'name',
                    'label'         => 'Sujet',
                    'empty_value'   => 'Sélectionne une valeur',
                    'empty_data'    => null,
                    'required'      => false,
                    'query_builder' => function (EntityRepository $repository) use ($field) {
                        $qb = $repository->createQueryBuilder('topic')
                            ->innerJoin('topic.field', 'field');
    
                        if($field instanceof Field) {
                            $qb = $qb->where('topic.field = :field')
                                ->setParameter('field', $field);
                        } elseif(is_numeric($field)) {
                            $qb = $qb->where('field.id = :field_id')
                                ->setParameter('field_id', $field);
                        } else {
                            $qb = $qb->where('0 = 1');
                        }
    
                        return $qb;
                    }
                )));
            };
    
            // Populate ddl to show form
            $country = $this->country;
            $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($refreshTopic, $refreshField, $factory, $country) {
                /** @var Lesson $data */
                $data = $event->getData();
                $form = $event->getForm();
    
                // Test if null because this event is called 2 times, only the second time with the actual Lesson object (which has null values in the creation case)
                if($data != null)
                    // In case of creation
                    if($data->getId()==null) {
                        // Creates empty fields
                        $refreshTopic($form, null);
                        $refreshField($form, null);
                    }
                    // In case of edition
                    else {
                        if ($data->getTopic() != null) {
                            $refreshTopic($form, $data->getTopic()->getField());
                            if ($data->getTopic()->getField() != null) {
                                $refreshField($form, $data->getTopic()->getField()->getClassYear());
                            }
                        }
                        else {
                            $refreshField($form, null);
                            $refreshTopic($form, null);
                        }
                    }
    
                /** @var FormFactory $factory */
                $form->add($factory->createNamed('classyear', 'entity', null, array(
                    'class'         => 'AAACoreBundle:ClassYear',
                    'property'      => 'name'.$country,
                    'mapped'        => false,
                    'label'         => 'Année',
                    'empty_value'   => 'Sélectionne une valeur',
                    'empty_data'    => null,
                    'required'      => false,
                    'query_builder' => function (EntityRepository $repository) {
                        return $repository->createQueryBuilder('classyear')
                            ->orderBy('classyear.sort');
                    }
                )));
            });
            // Populate ddl when form was posted
            $builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) use ($refreshTopic, $refreshField) {
                $form = $event->getForm();
                $data = $event->getData();
    
                if(array_key_exists('classyear', $data)) {
                    $refreshField($form, $data['classyear']);
                }
                if(array_key_exists('field', $data)) {
                    $refreshTopic($form, $data['field']);
                }
            });
    
            // Select value in ddl when editing
            $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($refreshTopic) {
                /** @var Lesson $data */
                $data = $event->getData();
                $form = $event->getForm();
                if (null === $data || null === $data->getId() ) {
                    return;
                }
    
                if ($data->getTopic() != null) {
                    $form->get('field')->setData($data->getTopic()->getField());
                    if ($data->getTopic()->getField() != null) {
                        $form->get('classyear')->setData($data->getTopic()->getField()->getClassYear());
                    }
                }
            });
        }
        public function getName()
        {
            return 'LessonDetailForm';
        }
        /** @param OptionsResolverInterface $resolver */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AAA\CoreBundle\Entity\Lesson'
            ));
        }
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to install and configure Symfony2 on my server. My website is on
I'm building a small website with Symfony2 (i.e. not Symfony 1.x). I used the
I'm building a small website with Symfony2 and Doctrine2. There are blog posts, events
I designing an programming a website using Symfony2. I completed some parts and checked
I am working on a website with symfony2, and in the back office I
I'm developing a website using symfony2 . First I started everything was fine, redirects
From the Jobeet tutorial provided in Symfony website, I found out that I can
I'm trying to find a way to integrate my website, coded using Symfony with
I'd like to write a new website from scratch. I'll be using PHP for
I am trying to create a website using Symfony and PostgreSQL. I cant find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.