From 85fc895a08109ec133aeea20670722f8cb4f2d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Bl=C3=A4ttermann?= Date: Thu, 28 Apr 2016 15:50:43 +0200 Subject: [PATCH] Possibility to override the label in country widget By passing in a closure into the form options it's possible to customize the way the label of the country field is formatted: --- Form/Type/PhoneNumberType.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Form/Type/PhoneNumberType.php b/Form/Type/PhoneNumberType.php index f94a814b..7fe31e74 100644 --- a/Form/Type/PhoneNumberType.php +++ b/Form/Type/PhoneNumberType.php @@ -64,10 +64,14 @@ public function buildForm(FormBuilderInterface $builder, array $options) if (false === isset($countries[$region])) { continue; } + + if (null !== $options['country_widget_label'] && is_callable($options['country_widget_label'])) { + $countryChoices[$options['country_widget_label']($countries, $name, $region)] = $region; + } else { + $countryChoices[sprintf('%s (+%s)', $name, $countries[$region])] = $region; + } - $countryChoices[sprintf('%s (+%s)', $name, $countries[$region])] = $region; } - $transformerChoices = array_values($countryChoices); $countryOptions = $numberOptions = array( @@ -141,6 +145,7 @@ public function configureOptions(OptionsResolver $resolver) 'by_reference' => false, 'error_bubbling' => false, 'country_choices' => array(), + 'country_widget_label' => null, 'preferred_country_choices' => array(), ) );