Mitch
Mitch

Reputation: 13

Symfony: How can I get full country name

I am using the CountryType field in Symfony. It now is displaying the country initials like NL for Netherlands. But I want to display the full country name.

How can I display the full country name instead of only country initials?

Code is as follows :

use Symfony\Component\Intl\Intl;
...

$countries = Intl::getRegionBundle()->getCountryNames();

$builder->add('companyaddresscountry', ChoiceType::class, array(
                'choices' => $countries, 'label'=>'Country'));

Upvotes: 1

Views: 3947

Answers (2)

Laurynas
Laurynas

Reputation: 796

Your choices are not 'as values'. You can either flip your array using php's function array_flip

$countries = Intl::getRegionBundle()->getCountryNames();

$builder->add('companyaddresscountry', ChoiceType::class, array(
    'choices' => array_flip($countries),
    'label'=>'Country'
));

or add parameter to your form field 'choices_as_values' (this option is deprecated):

This option is deprecated and you should remove it from your 3.x projects (removing it will have no effect). For its purpose in 2.x, see the 2.7 documentation.

$countries = Intl::getRegionBundle()->getCountryNames();

$builder->add('companyaddresscountry', ChoiceType::class, array(
    'choices' => $countries,
    'label'=>'Country',
    'choices_as_values' => true
));

Upvotes: 3

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

Inside array $countries you could have an array where the key is only 2 letters but the value is the full name like this:

$countries 

equals to

array('AF' => 'Afghanistan', ...)

So you can parse your array and get only values for the full name

$fullname = [];
foreach ($countries as $key => $value) {
    $fullname[] = $value;
}

Upvotes: 0

Related Questions