Search code examples
symfonylocale

Symfony2 locale type form element with limited list


I want to add in my MyType form locale field, so I put:

$builder->add('locale', 'locale', array(
  'label'       => 'user.locale',
  'required'    => true,
));

However it rendered me a whole list of available locales because default choices array for this type is:

'choices' => Intl::getLocaleBundle()->getLocaleNames()

I would like to display only en, de and pl. How can I limit the output to those languages ?

The best solution would be with defining this list in config.yml.


Solution

  • Use this:

    $builder->add('locale', 'locale', array(
        'label'     => 'user.locale',
        'required'  => true,
        'choices'   => array('en' => 'English', 'de' => 'Deutsch', 'pl' => 'Polish')
    ));
    

    See the documentation for getting these values from configuration.

    Edit : Thanks to Djuro Mandinic for pointing the error in my previous answer, the choices array must contain keys and values.