1

I am working on a Symfony2 CRM which implements Doctrine ORM for the forms. One of the forms on the CRM is for creating customer addresses, and has all the standard text fields such as street, city, postcode etc. but also a dropdown for country and county. The problem is, if I retrieve all counties from the database the list is huge and not only takes ages to load but it's very difficult for my client to find the county since it's for every country in the world.

What I'd like is for them to select the country first and then the county field is auto populated based on the selected country. However, I am unsure how to do this in Doctrine.

Here is my buildForm function in the form type (noting that here, Zone means County):

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('company' , 'text', array('required' => false ));
    $builder->add('firstname' , 'text', array('required' => true, 'label' => 'First name' ));
    $builder->add('lastname' , 'text', array('required' => true, 'label' => 'Last name' ));
    $builder->add('address1' , 'text');
    $builder->add('address2' , 'text', array('required' => false ));
    $builder->add('city' , 'text');
    $builder->add('country' , 'entity',
        array(
            'class' => 'AppBundle:Oc49Country',
            'property' => 'name',
            'empty_value' => 'Choose an option',
        ));
    $builder->add('zone' , 'entity',
        array(
            'class' => 'AppBundle:Oc49Zone',
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('z')
                    ->orderBy('z.name', 'ASC');
            },
            'empty_value' => '-- Please select --',
            'required' => true,
            'label' => 'County/State'
        ));
    $builder->add('postcode' , 'text');
    $builder->add('save', 'submit', array(
        'attr' => array(
            'class' => 'btn btn-primary'
        ),
    ));
}

Is there a way of adding an additional parameter to the query builder in order to filter the county list, on the fly?

Michael Emerson
  • 1,774
  • 4
  • 31
  • 71

1 Answers1

0

Since you want to change the County select list after the page is already rendered by symfony, you'll have to use ajax in some way to update just that county select list.

I would use an jquery/ajax call to populate the County select list based on the selected country. From your twig template, add some a jquery function along these lines:

$('#country_select).on('change', function() {
    url: '/path/to/controller/to/get/countyList',
    method: 'GET'
}).done(function() {
    // update options
});

This question will help with details. Obviously my code is rough, but hopefully this helps.

Community
  • 1
  • 1
ehymel
  • 1,360
  • 2
  • 15
  • 24
  • I know how to do it this way, I've implemented this kind of thing before - but I'm trying to find a way to do it using the Doctrine method. I wondered if there was some way to add a Listener to check the selected country, but I don't really know how I'd do that... – Michael Emerson Mar 15 '17 at 09:19
  • Actually, come to think of it, I think I will just implement this but disable the select box until it gets re-populated. Thanks – Michael Emerson Mar 15 '17 at 10:28
  • The closest thing I see that come close is this: http://symfony.com/doc/current/form/dynamic_form_modification.html. See option 3. I don't think this is exactly what you are wanting though. I've not tried it myself so maybe I'm wrong. – ehymel Mar 16 '17 at 00:50
  • at this day, this post should be useful : https://stackoverflow.com/questions/71264515/symfony5-form-multiple-select-choices-list-data-auto-update-with-ajax – bcag2 Mar 16 '22 at 16:57