Last Updated: March 27, 2016
·
3.578K
· pleone

Symfony2 country field multiple choices

What if you need to allow user to select more than 1 country ?

Pretty simple, using the country field type. In the entity we do:

/**
 * @var string
 *
 * @ORM\Column(name="country", type="simple_array", length=255, nullable=true)
 */
private $country;  

So our type is set to array. Doctrine will take care of serialize/unserialize, so the getter will return an array easy to use.

If you use sonata, you can add this form type to have multiple choices:

->add('country','country',array('multiple'=>true)); 

Done! ;)