Easyadminbundle: Can't order form fields after overriding someone

Created on 17 Jun 2016  路  9Comments  路  Source: EasyCorp/EasyAdminBundle

My issue is when I override createForms method, the fields I override are showed in the bottom of the form and I can't order them as I want

I want this order in my form:
screen shot 2016-06-17 at 01 27 39

These the override createForm method:
screen shot 2016-06-17 at 01 27 54

And this is what I get (note that the overriden fields are showed in the bottom of the form):
screen shot 2016-06-17 at 01 28 16

Sorry about paste pictures, but it is too hard indent code here :/

Thanks for your time!

Most helpful comment

@JuanWilde alternatively you can use this bundle to arrange fields for dynamic form types. https://github.com/egeloen/ivory-ordered-form/blob/master/doc/usage.md#position

However, these fields that already exists you don't need removed, these keeps the old position (in children array) and the type and options are changed only.

All 9 comments

Using a form theme is the best way to ensure the fields rendering order.

Yep, I know that method, even I thought removing and adding all my custom form fields (image 2). But I want to know if there is a way to do this in a YML file like config.yml or another one.

Anyway, thanks for your answer.

@JuanWilde We add form fields in the order you set them in the config, but if some custom code remove and add them back, it can't keep the order. Due to how symfony forms work, an added field is always added after the previously added ones.
Anyway, relying on this particular order is not the right thing to do (because the fields order in a form type is not pertinent).

Thus the form theme suggestion, which is the only one IMHO 馃槃
But... I think we could do something to ease writing form theme for this case...

The block prefix to use to override the form theme is always the same: easyadmin_widget. Thus you cannot deal easily with theme overriding for a particular entity.

We should probably allow to override those lines easily by wrapping them in a proper block. Then you can override it and rely on the easyadmin.entity.name variable to render another dedicated block with the right fields order.

EDIT: The other solution is to let the EasyAdminFormType change the FormView children order (probably more straightforward actually).

@JuanWilde alternatively you can use this bundle to arrange fields for dynamic form types. https://github.com/egeloen/ivory-ordered-form/blob/master/doc/usage.md#position

However, these fields that already exists you don't need removed, these keeps the old position (in children array) and the type and options are changed only.

However, these fields that already exists you don't need removed, these keeps the old position (in children array) and the type and options is changed only.

@yceruto : 馃槷 I didn't event thought about that. 馃憤

For instance:

$form->add('name', TextType::class);
$form->add('age', NumberType::class);
//...
$form->add('name', TextType::class, ['label' => 'First Name']);

the results is always two fields: first name with label: "First Name" and second age.

@yceruto. Looks like your solution is the less-code one! I saw that is not necessary to remove field before adding again. So, now the method looks like this

image

Thanks a lot! :+1:

I think we can close this issue as "fixed" thanks to the help provided by @ogizanagi and @yceruto. Thanks!

I ran into similar problem, I'm commenting here if anybody has same need as I did :

I needed to use 'query_builder' option for one of the entity properties but I needed to pass parameters to this query builder, that's how I proceeded :

  1. Add the property in config
    - { property: 'exempleProperty', type: entity, type_options: { required: true, attr: { data-widget: 'select2' } } }
    protected function createEntityFormBuilder($entity, $view)
    {
        $formBuilder = parent::createEntityFormBuilder($entity, $view);

        //get old field
        $oldExampleProperty = $formBuilder->get('exampleProperty');
        /*
        * Replace old options you need to override (in my case the 'choice_loader')
        * Remember that setting 'query_builder' here won't have any effect on loaded data
        * as easyadmin uses 'ChoiceType' when you set 'type: entity' in config.yml
        * so you have to use 'choice_loader'
        */
        $options = array_replace($oldExampleProperty->getOptions(), array(
            //Using lazy loading to load choices
            'choice_loader' => new CallbackChoiceLoader(function() {
                //You can access Controller properties here,  so let's use it to get our repository
                $er = $this->em->getRepository(ExampleProperty::class);
                //the parameter(s) you need to pass to your repository method
                $exampleParameter = 3;
                $anotherParameter = 'Symfony';
                return $er->getDescendants($exampleParameter, $anotherParameter);
            }),
        ));

        $newExampleProperty = $formBuilder->getFormFactory()->createNamedBuilder('exampleProperty', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, null, $options);

        /*
        * this is for design, if your property was in a group or tab (as you configured it in config.yml) you have
        * to reset these, we just get these from the builder resolved by easyadmin
        * (these attributes have to be sat after creation)
        */
        $newExampleProperty->setAttribute('easyadmin_form_tab', $oldExampleProperty->getAttribute('easyadmin_form_tab'));
        $newExampleProperty->setAttribute('easyadmin_form_group', $oldExampleProperty->getAttribute('easyadmin_form_group'));
        $formBuilder->add($newExampleProperty);

        return $formBuilder;
    }
Was this page helpful?
0 / 5 - 0 ratings