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 9191485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:41:43+00:00 2026-06-17T20:41:43+00:00

I am confronted to a problem that is driving me crazy for more than

  • 0

I am confronted to a problem that is driving me crazy for more than 3 days and I do not find any solutions. Nevertheless I found a post on stackoverflow that is EXCACTLY the problem I am facing. Unfortunately the person did manage to find a solution on his down but he or she did not shared it fully. As he explained it perfectly let just copy paste it here below:
By the way it seems that person who created that post only created his account for this problem and never came back since for other things. That it is why I allow myself to ask here again…

I have 2 entities (A and B) with a Many to One relationship between
them.

I create my form with the A entity and i use an entity field (dropdown
list) to display the rows in the B entity. I use a query builder to
filter them. If don’t change the values in the list (ie. with ajax),
everything is working fine.

But if I change dynamicly the values in the dropdown, when I submit
the form I have this error “This value is invalid”

It’s because the submitted value isn’t included in the “array”
returned by the query builder.

It seems that this validation is automatic in symfony for entity field
(I don’t use any asserts on this field). I’d like to get rid of this.
But how ?

It seems that I need to implement Form Events. Unfortunatally I do not get it. I read the documentation which is very poor on that subject, read a lot of posts, searched on the Internet but did not found anything.

Here below my personal form type. What I do is the following. I create the first entity field type with the mapped property set to false and filter the entity just to get the departements. Then I create another entity type called localisation. By default I filter the entity to get nothing (”). What I do then to populate it is to use Jquery. But unfortunatelly I am confro,ted to the same problem as the other buddy (see above).

use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Doctrine\ORM\EntityRepository;
use Auth\GeoBundle\Form\LocalisationType;


class RegistrationFormType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->add('departement', 'entity', array(
            'mapped' => false,
            'empty_value' => '',
            'class' => 'AuthGeoBundle:Localisation',
            'property' => 'departement',
            'query_builder' => function ($repository) {
                return $repository
                    ->createQueryBuilder('e')
                    ->add('groupBy', 'e.departement')
                ;
            },
        ));


        $builder->add('localisation', 'entity', array(
            'empty_value' => '',
            'class' => 'AuthGeoBundle:Localisation',
            'property' => 'formLabel',
            'query_builder' => function ($repository) use ($dpt) {
                return $repository
                    ->createQueryBuilder('e')
                    ->where('e.departement = :dpt')
                    ->setParameter('dpt', '')
                    ->add('orderBy', 'e.ville ASC')
                ;
            },
        ));

        //some other fields here...
    }

    public function getName()
    {
        return 'auth_user_registration';
    }
}
  • 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-17T20:41:44+00:00Added an answer on June 17, 2026 at 8:41 pm

    I finally manage to find a solution using the form events. I played with the “tutorial” at http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html and got it working 🙂 Here below the code I used in case somebody interested.

    My formType:

    <?php
    //src/Auth/UserBundle/Form/Type/RegistrationFormType.php
    namespace Auth\UserBundle\Form\Type;
    
    use Symfony\Component\Form\FormBuilderInterface;
    use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
    use Doctrine\ORM\EntityRepository;
    use Auth\GeoBundle\Form\LocalisationType;
    
    use Auth\UserBundle\Form\EventListener\IsAdminFieldSubscriber;
    
    class RegistrationFormType extends BaseType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            parent::buildForm($builder, $options);
    
            //NE PAS EFFACER -> exempled e comment ajouter un champ qui n'a rien à voir avec nos entitys
            //$builder->add("firstName", "text", array("mapped" => false));
    
            $builder->add('departement', 'genemu_jqueryselect2_entity', array(
                'mapped' => false,
                'empty_value' => '',
                'class' => 'AuthGeoBundle:Localisation',
                'property' => 'departement',
                'query_builder' => function ($repository) {
                    return $repository
                        ->createQueryBuilder('e')
                        ->add('groupBy', 'e.departement')
                    ;
                },
            ));
    
    
            $dpt = "";
            $builder->add('localisation', 'genemu_jqueryselect2_entity', array(
                'empty_value' => '',
                'class' => 'AuthGeoBundle:Localisation',
                'property' => 'formLabel',
                'query_builder' => function ($repository) use ($dpt) {
                    return $repository
                        ->createQueryBuilder('e')
                        ->where('e.departement = :dpt')
                        ->setParameter('dpt', $dpt)
                        ->add('orderBy', 'e.ville ASC')
                    ;
                },
            ));
    
            $builder->add('sexe', 'genemu_jqueryselect2_choice', array(
                'empty_value' => '',
                'choices'   => array(
                    'homme'   => 'Homme',
                    'femme' => 'Femme',
                ),
                'configs' => array(
                    'minimumResultsForSearch' => 5,
                )
            ));
    
            $builder->add('date_naissance', 'date', array(
                'empty_value' => '',
                'widget' => 'choice',
                'attr' => array('class' => 'input-small'),
                'years' => range(1900,2100),
                'months' => range(1,12),
                'days' => range(1,31),
            ));
    
            $builder->add('petit_mot');
    
            $subscriber = new IsAdminFieldSubscriber($builder->getFormFactory());
            $builder->addEventSubscriber($subscriber);
        }
    
    
        public function getName()
        {
            return 'auth_user_registration';
        }
    }
    

    my EventListener:

    <?php
    //src/Auth/UserBundle/Form/EventListener/isAdminFieldSubscriber.php
    namespace Auth\UserBundle\Form\EventListener;
    
    use Symfony\Component\Form\Event\DataEvent;
    use Symfony\Component\Form\FormFactoryInterface;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\Form\FormEvents;
    
    class IsAdminFieldSubscriber implements EventSubscriberInterface
    {
        /**
         * @var FormFactoryInterface 
         */
        private $factory;
    
        /**
         * @param FormFactoryInterface $factory 
         */
        public function __construct(FormFactoryInterface $factory)
        {
            $this->factory = $factory;
        }
    
        /**
         * @return array
         */
        public static function getSubscribedEvents()
        {
            return array(
                FormEvents::PRE_BIND => 'preBind',
            );
        }
    
        /**
         * Called before form data is set
         *
         * @param DataEvent $event
         */
        public function preBind(DataEvent $event)
        {
            $data = $event->getData();
            $form = $event->getForm();
    
            if (null === $data) {
                return;
            }
    
            $dpt = $data['localisation'];
    
            $form->add($this->factory->createNamed('localisation', 'entity', null, array(
                'empty_value' => '',
                'class' => 'AuthGeoBundle:Localisation',
                'property' => 'formLabel',
                'query_builder' => function ($repository) use ($dpt)  {
                    return $repository
                        ->createQueryBuilder('e')
                        ->where('e.id = :dpt_id')
                        ->setParameter('dpt_id', $dpt)
                        ->add('orderBy', 'e.ville ASC')
                    ;
                },
            )));
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am confronted to a problem that is driving me crazy. I really do
We've all been confronted to that problem : Entity must be managed when we
I wrote a package, and I confronted does not name a type problem while
Actually I am confronted with a Problem. I've got a .apk-File in one Package
I'm confronted by a strange jQuery problem. I was asked to help figure out
Ok, a friend of mine confronted me with a regular expression he found in
I am confronted with a new kind of problem which I haven't encountered yet
I see myself regularly confronted with the following problem. I have some kind of
i have just confronted this situation which i am not able to understand: In
I found myself confronted with an interview question where the goal was to write

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.