Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a199522

Browse filesBrowse files
committed
[Form] Allow to configure selectable countries
1 parent f05a1ca commit a199522
Copy full SHA for a199522

File tree

3 files changed

+28
-3
lines changed
Filter options

3 files changed

+28
-3
lines changed

‎src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/CountryType.php
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
18+
use Symfony\Component\Form\Extension\Core\ArrayInclusionFilter;
1819
use Symfony\Component\Intl\Intl;
1920
use Symfony\Component\OptionsResolver\Options;
2021
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -42,16 +43,26 @@ public function configureOptions(OptionsResolver $resolver)
4243
$resolver->setDefaults(array(
4344
'choice_loader' => function (Options $options) {
4445
$choiceTranslationLocale = $options['choice_translation_locale'];
46+
$supportedCountries = $options['supported_countries'];
4547

46-
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47-
return array_flip(Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale));
48+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $supportedCountries) {
49+
$countries = Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale);
50+
51+
if (null !== $supportedCountries) {
52+
$supportedCountries = array_map('strtoupper', $supportedCountries);
53+
$countries = array_filter($countries, new ArrayInclusionFilter($supportedCountries), ARRAY_FILTER_USE_KEY);
54+
}
55+
56+
return array_flip($countries);
4857
});
4958
},
5059
'choice_translation_domain' => false,
5160
'choice_translation_locale' => null,
61+
'supported_countries' => null,
5262
));
5363

5464
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
65+
$resolver->setAllowedTypes('supported_countries', array('null', 'string[]'));
5566
}
5667

5768
/**

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ public function testChoiceTranslationLocaleOption()
5757
$this->assertContains(new ChoiceView('MY', 'MY', 'Малайзія'), $choices, '', false, false);
5858
}
5959

60+
public function testAcceptedCountriesIsConfigurable()
61+
{
62+
$choices = $this->factory
63+
->create(static::TESTED_TYPE, null, array(
64+
'supported_countries' => array('GB', 'DE'),
65+
))
66+
->createView()->vars['choices'];
67+
68+
$this->assertContains(new ChoiceView('DE', 'DE', 'Germany'), $choices, '', false, false);
69+
$this->assertContains(new ChoiceView('GB', 'GB', 'United Kingdom'), $choices, '', false, false);
70+
$this->assertNotContains(new ChoiceView('US', 'US', 'United States'), $choices, '', false, false);
71+
$this->assertNotContains(new ChoiceView('FR', 'FR', 'France'), $choices, '', false, false);
72+
}
73+
6074
public function testUnknownCountryIsNotIncluded()
6175
{
6276
$choices = $this->factory->create(static::TESTED_TYPE, 'country')

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/LocaleTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/LocaleTypeTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testLocalesAreSelectable()
3737

3838
public function testSupportedLocalesAreConfigurable()
3939
{
40-
$choices = $this->factory->create(static::TESTED_TYPE, null, ['supported_locales' => ['en', 'zh_Hant_MO']])
40+
$choices = $this->factory->create(static::TESTED_TYPE, null, array('supported_locales' => array('en', 'zh_Hant_MO')))
4141
->createView()->vars['choices'];
4242

4343
$this->assertContains(new ChoiceView('en', 'en', 'English'), $choices, '', false, false);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.