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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:26:19+00:00 2026-05-29T06:26:19+00:00

How to use related select boxes in Symfony ? Let’s say, I have a

  • 0

How to use related select boxes in Symfony ?

Let’s say, I have a select list containing compagnies and another containing employees of the selected company. How do I define those in Symfony?
I have already created all the Javascript related code but when submitting my form and having errors on some fields, the all “sub” select fields are reset to null.

Any ideas?
Thanks,

EDIT : As the question seems to be misunderstood, I’ll add some precisions :

Description :

  1. I have an entity Company containing a list of Employees using a @OneToMany relation.
  2. When I select a company in a select/dropdown list, the second dropdown list containing the employees is updated via jQuery. That part is done, works perfectly
  3. When submitting the form without errors, the entity form solution works fine.
  4. When submitting the form containing errors, the second dropdown list is containing all possible values. They are not filtered on the selected company.

Tried solutions :

  • My first idea was to use the form entity type, thinking the component could be binded somehow on another field. ie. update list of employees based on the value of the selected company.

Not working, there is no way out of the box to do this. Even non out of the box solutions…

  • Then I thought about manually passing the selected company as parameter to the query builder of the second dropdown list.

But when the form is created, the values are empty. The values are only set on bindRequest.

  • Thought about using the choice type. Delegating all filter functionnality to the UI via Javascript. Meaning when the page loads, an empty list appears and is populated by Javascript based on the selected company.

This actually works, but I think there is no other word than really really ugly programming here.

PS :

Question has been asked here, on the Symfony2 mailing-list, on Twitter and the officiel Symfony 2 forum. I have of course searched each of those several times before posting my questions.

  • 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-05-29T06:26:20+00:00Added an answer on May 29, 2026 at 6:26 am

    Concerning what you already tried, I think you should retry your first/second ideas:

    My first idea was to use the form entity type, thinking the component
    could be binded somehow on another field. ie. update list of employees
    based on the value of the selected company.

    You can populate the employees select box using the entity type.
    All you have to do is to define the good options:

    class FooType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder
                ->add('employee', 'entity', array(
                    'class' => 'Entity\Employee',
                    'query_builder' => function ($repository) use($options) {
                        return $repository
                            ->createQueryBuilder('e')
                            ->where('e.company = :company')
                            ->setParameter('company', $options['companyId'])
                        ;
                    },
                ))
            ;
        }
    
        public function getDefaultOptions(array $options)
        {
            return array('data_class' => 'Entity\Foo', 'companyId' => null);
        }
    }
    

    Then I thought about manually passing the selected company as
    parameter to the query builder of the second dropdown list.

    The example here filters the employees list based on the companyId form’s option.
    You can modify this behavior by filtering directly on the company present in the form’s data.

    public function buildForm(FormBuilder $builder, array $options)
    {
        $companyId = $builder->getData()->getCompanyId();
        $builder
            ->add('employee', 'entity', array(
                'class' => 'Entity\Employee',
                'query_builder' => function ($repository) use ($companyId) {
                    return $repository
                        ->createQueryBuilder('e')
                        ->where('e.company = :company')
                        ->setParameter('company', $companyId)
                    ;
                },
            ))
        ;
    }
    

    You still have to implement the getEmployee() and setEmployee() methods in your Entity\Foo class.

    But when the form is created, the values are empty. The values are only set on
    bindRequest.

    No. Values are set when you create you form using form factory (third argument),
    OR when you call $form->setData($foo);. Data is modified when you bind new inputs to the form.

    There could be a problem with this approach: it’s possible that the employee id bound to the form is not available in the form choice list, because you changed the company (and thus the employees).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Related: How can I use polymorphism in XML Serialization? I have a class I
I have a lookup field in the form, before select the related entity for
In my controller i am trying to use include with EF4 to select related
I have a strange issue today related to the oci_bind_by_name function I use in
I have a OrganisationName select box in Edit/Add User Page. <s:select key=user.organisationname name=entity.organisationId list=organisationList
Trying not to select same connection multiple times I have tried to use charindex()
This I think is related to my use of the nlog C++ API (and
Related to: Query Windows Search from Java But this time to use OSX's spotlight
I found this related question: How do I use composition with inheritance? I would
This is related to a question I asked here: Thread Locking in Ruby (use

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.