Symfony2 : Sort / Order a translated entity form field?

Seb33300

I am trying to order an entity form field witch is translated.

I am using the symfony translation tool, so i can't order values with a SQL statement. Is there a way to sort values after there are loaded and translated ?

Maybe using a form event ?

$builder
    ->add('country', 'entity', 
            array(
                'class' => 'MyBundle:Country',
                'translation_domain' => 'countries',
                'property' => 'name',
                'empty_value' => '---',
            )
        )
Seb33300

I found the solution to sort my field values in my Form Type.

We have to use the finishView() method which is called when the form view is created :

<?php

namespace My\Namespace\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;

class MyFormType extends AbstractType
{
    protected $translator;

    public function __construct(Translator $translator)
    {
        $this->translator = $translator;
    }

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        // Order translated countries
        $collator = new \Collator($this->translator->getLocale());
        usort(
            $view->children['country']->vars['choices'], 
            function ($a, $b) use ($collator) {
                return $collator->compare(
                    $this->translator->trans($a->label, array(), 'countries'), 
                    $this->translator->trans($b->label, array(), 'countries')
                );
            }
        );
    }

    // ...

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('country', 'entity', 
                    array(
                        'class' => 'MyBundle:Country',
                        'translation_domain' => 'countries',
                        'property' => 'name',
                        'empty_value' => '---',
                    )
                )
        ;
    }

}

OLD ANSWER

I found a solution for my problem, I can sort them in my controller after creating the view :

$fview = $form->createView();
usort(
    $fview->children['country']->vars['choices'], 
    function($a, $b) use ($translator){
        return strcoll($translator->trans($a->label, array(), 'countries'), $translator->trans($b->label, array(), 'countries'));
    }
);

Maybe I can do that in a better way ? Originally I wished to do directly in my form builder instead of adding extra code in controllers where I use this form.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony2 : Sort / Order a translated entity form field?

From Dev

Symfony2 form with field not in entity

From Dev

Symfony2 form entity field with many-to-many relationship

From Dev

Symfony2: Entity form field with empty value

From Dev

Additional properties to entity Field Type in a form in Symfony2

From Dev

Setting a Symfony2 Entity form field using it's id

From Dev

Assign filtered list of entities to Entity field in Symfony2 form

From Dev

Symfony2 entity form field and its attribute name

From Dev

Symfony2 Form field Entity with 170k of results

From Dev

How to sort by / order by a field of a nested entity?

From Dev

Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

From Dev

symfony2 - How to create a form field of type "entity" without values

From Dev

Symfony2 - form entity field with multiple , data not preserved after choices set

From Dev

How to order translated with not translated fields using doctrine knp translatable extentions with A2lix translation form?

From Dev

Entity Field type hidden in Symfony2

From Dev

Symfony2 FormType entity field type

From Dev

Validation Symfony2 Entity Choice Field

From Dev

Symfony2 field order in database table

From Dev

Symfony2 field order in database table

From Dev

Entity Not Found error in symfony2 Form

From Dev

Dynamic form (switch entity) symfony2

From Dev

Order of Symfony form CollectionType field

From Dev

Override form field templates in Symfony2

From Dev

Is there a way to rename a field in a form in Symfony2?

From Dev

Symfony2 errors on form field and parent

From Dev

Override form field templates in Symfony2

From Dev

Symfony2 form - Remove related entity form on submit

From Dev

Symfony unmapped entity form field not having data

From Dev

Symfony 2 form conditional required field with fields not mapped to entity

Related Related

  1. 1

    Symfony2 : Sort / Order a translated entity form field?

  2. 2

    Symfony2 form with field not in entity

  3. 3

    Symfony2 form entity field with many-to-many relationship

  4. 4

    Symfony2: Entity form field with empty value

  5. 5

    Additional properties to entity Field Type in a form in Symfony2

  6. 6

    Setting a Symfony2 Entity form field using it's id

  7. 7

    Assign filtered list of entities to Entity field in Symfony2 form

  8. 8

    Symfony2 entity form field and its attribute name

  9. 9

    Symfony2 Form field Entity with 170k of results

  10. 10

    How to sort by / order by a field of a nested entity?

  11. 11

    Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

  12. 12

    symfony2 - How to create a form field of type "entity" without values

  13. 13

    Symfony2 - form entity field with multiple , data not preserved after choices set

  14. 14

    How to order translated with not translated fields using doctrine knp translatable extentions with A2lix translation form?

  15. 15

    Entity Field type hidden in Symfony2

  16. 16

    Symfony2 FormType entity field type

  17. 17

    Validation Symfony2 Entity Choice Field

  18. 18

    Symfony2 field order in database table

  19. 19

    Symfony2 field order in database table

  20. 20

    Entity Not Found error in symfony2 Form

  21. 21

    Dynamic form (switch entity) symfony2

  22. 22

    Order of Symfony form CollectionType field

  23. 23

    Override form field templates in Symfony2

  24. 24

    Is there a way to rename a field in a form in Symfony2?

  25. 25

    Symfony2 errors on form field and parent

  26. 26

    Override form field templates in Symfony2

  27. 27

    Symfony2 form - Remove related entity form on submit

  28. 28

    Symfony unmapped entity form field not having data

  29. 29

    Symfony 2 form conditional required field with fields not mapped to entity

HotTag

Archive