Symfony2 choice widget ordering of preferred_choices

5.8k Views Asked by At

I have a registration form with a country select. Most of the users are from the UK, and then a way behind them are from Ireland. The rest of the world together are a very small proportion at the moment. Therefore I would like to put UK and Ireland at the top, as preferred choices:

->add('country', 'choice', array(
    'choices' => Locale::getDisplayCountries(\Locale::getDefault()),
    'preferred_choices' => array('GB', 'IE')
))

This works ok, BUT "Ireland" appears above "United Kingdom" in the separated, preferred options.

Is there any way to set the order of these so that UK appears top of the list?

3

There are 3 best solutions below

0
On BEST ANSWER

Created issue:

https://github.com/symfony/symfony/issues/5136

Hopefully will be looked at in sf 2.2

1
On

Have you checked this article?

You could guild your form by analogy to:

$builder->add('users', 'entity', array(
    'class' => 'AcmeHelloBundle:User',
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('u')
            ->orderBy('u.username', 'ASC');
    },
));
0
On

Until this is fixed, you can use a little JavaScript to at least force a default. Example which uses JQuery:

<script type="text/javascript">
/* Set the default country to US */
$(document).ready(function() {
  $("select option").filter(function() {
      //may want to use $.trim in here
      return $(this).text() == "United States"; 
  }).prop('selected', true);
});
</script>