1

I have an array:

[ 0 => 'translate.label1', 1 => 'translate.label2', 2 => 'translate.label1' ]

which I specify as a choices param for field creation.

Now, is it possible to sort these options by translated labels? For example, label1 translated value is zero, label2 - something, label3 - abracadabra. Of course, I expect select options to be:

  • abracadabra
  • something
  • zero

I thought to use this approach: https://stackoverflow.com/a/21586886/2324004

But doesn't it involve second translator digest inside view? Any ideas? I'd rather to use something more flexible because my form is based on non-fixed selects count and it would have executed something recursive...

Community
  • 1
  • 1
eRIZ
  • 1,477
  • 16
  • 32

2 Answers2

0

You have to do it in js after data will be uploaded.

Check this topic:

Javascript to sort contents of select element

Community
  • 1
  • 1
Tracha
  • 123
  • 8
0

Due to fact the translation occurs inside Twig view, the only solution (right now I'm aware) is to prepare translated and sorted list during form build process by injecting manually TranslatorInterface service.

Unfortunately, without overriding widget's template, translation occurs second time, against already translated labels.

eRIZ
  • 1,477
  • 16
  • 32
  • 1
    Actually you can disable translation of choices in the view: http://symfony.com/blog/new-in-symfony-2-7-form-and-validator-updates#added-choice-translation-domain-domain-to-avoid-translating-options – Tobias Schultze Sep 10 '15 at 15:12