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?