Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default_region as selected country in widget country choice #119

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Form/Type/PhoneNumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions Tests/Form/Type/PhoneNumberTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ public function testCountryChoiceTranslations()
$this->assertFalse($view['country']->vars['choice_translation_domain']);
}

/**
* @dataProvider countryProvider
*/
public function testDefaultRegionAsSelectedInCountryWidget($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));

$countryViewData = $form->get('country')->getViewData();

$this->assertEquals($country, $countryViewData);
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
Expand Down Expand Up @@ -219,4 +235,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')
);
}
}