From e1e2aff10070a8638ba6fcd6d6c263e2ba820618 Mon Sep 17 00:00:00 2001 From: Mame Medoune Diop Date: Mon, 30 Jan 2017 12:40:26 +0000 Subject: [PATCH 1/4] Set default_region as selected country in widget country choice --- Form/Type/PhoneNumberType.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Form/Type/PhoneNumberType.php b/Form/Type/PhoneNumberType.php index 58b67bf0..f1be865a 100644 --- a/Form/Type/PhoneNumberType.php +++ b/Form/Type/PhoneNumberType.php @@ -96,6 +96,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) $countryOptions['required'] = true; $countryOptions['choices'] = $countryChoices; $countryOptions['preferred_choices'] = $options['preferred_country_choices']; + if($options['default_region']!= PhoneNumberUtil::UNKNOWN_REGION) { + $countryOptions['data'] = $options['default_region']; + } + $builder ->add('country', $choiceType, $countryOptions) From bfe4df1b03042a41281fdc1312ee0c96d12153b7 Mon Sep 17 00:00:00 2001 From: Mame Medoune Diop Date: Sun, 21 Jan 2018 15:50:49 +0000 Subject: [PATCH 2/4] Added Tests --- Tests/Form/Type/PhoneNumberTypeTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Tests/Form/Type/PhoneNumberTypeTest.php b/Tests/Form/Type/PhoneNumberTypeTest.php index aadf22ea..34be0f44 100644 --- a/Tests/Form/Type/PhoneNumberTypeTest.php +++ b/Tests/Form/Type/PhoneNumberTypeTest.php @@ -188,6 +188,24 @@ public function testCountryChoiceTranslations() $this->assertFalse($view['country']->vars['choice_translation_domain']); } + /** + * @dataProvider + */ + public function testDefaultRegionAsSelectedInCountryWidget() + { + $defaultRegion = 'GB'; + $form = $this->factory->create( + PhoneNumberType::class, + null, + array( + 'widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, + 'default_region' => $defaultRegion, + ) + ); + + + $this->assertEquals($defaultRegion, $form->get('country')->getViewData()); + } /** * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException */ From 6b54ac0a51eb48908566986b652fce50140e111a Mon Sep 17 00:00:00 2001 From: Mame Medoune Diop Date: Sun, 21 Jan 2018 16:03:45 +0000 Subject: [PATCH 3/4] Added data provider and fixed travis error --- Tests/Form/Type/PhoneNumberTypeTest.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Tests/Form/Type/PhoneNumberTypeTest.php b/Tests/Form/Type/PhoneNumberTypeTest.php index 34be0f44..56ae0f57 100644 --- a/Tests/Form/Type/PhoneNumberTypeTest.php +++ b/Tests/Form/Type/PhoneNumberTypeTest.php @@ -189,22 +189,21 @@ public function testCountryChoiceTranslations() } /** - * @dataProvider + * @dataProvider countryProvider */ - public function testDefaultRegionAsSelectedInCountryWidget() + public function testDefaultRegionAsSelectedInCountryWidget($country) { - $defaultRegion = 'GB'; $form = $this->factory->create( PhoneNumberType::class, null, array( 'widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, - 'default_region' => $defaultRegion, + 'default_region' => $country, ) ); - $this->assertEquals($defaultRegion, $form->get('country')->getViewData()); + $this->assertEquals($country, $form->get('country')->getViewData()); } /** * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException @@ -237,4 +236,18 @@ private function createChoiceView($label, $code) return new $class($code, $code, $label); } + + public function countryProvider() + { + return array( + array('AF'), + array('AO'), + array('BZ'), + array('CZ'), + array('FR'), + array('GR'), + array('KG'), + array('MQ') + ); + } } From 8e0b7850e302044197ca6a33761ff07c28e11d7c Mon Sep 17 00:00:00 2001 From: Mame Medoune Diop Date: Sun, 21 Jan 2018 16:17:04 +0000 Subject: [PATCH 4/4] Added data provider and fixed travis error --- Tests/Form/Type/PhoneNumberTypeTest.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Tests/Form/Type/PhoneNumberTypeTest.php b/Tests/Form/Type/PhoneNumberTypeTest.php index 56ae0f57..52cecd1d 100644 --- a/Tests/Form/Type/PhoneNumberTypeTest.php +++ b/Tests/Form/Type/PhoneNumberTypeTest.php @@ -193,17 +193,16 @@ public function testCountryChoiceTranslations() */ public function testDefaultRegionAsSelectedInCountryWidget($country) { - $form = $this->factory->create( - PhoneNumberType::class, - null, - array( - 'widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, - 'default_region' => $country, - ) - ); + if (method_exists('Symfony\\Component\\Form\\FormTypeInterface', 'getName')) { + $type = new PhoneNumberType(); + } else { + $type = 'Misd\\PhoneNumberBundle\\Form\\Type\\PhoneNumberType'; + } + $form = $this->factory->create($type, null, array('widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, 'default_region' => $country)); - - $this->assertEquals($country, $form->get('country')->getViewData()); + $countryViewData = $form->get('country')->getViewData(); + + $this->assertEquals($country, $countryViewData); } /** * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException