Last Updated: March 27, 2016
·
4.033K
· 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! ;)

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside