Skip to content

[Form] Change the order of fields #5827

Closed
@tmartin

Description

@tmartin

Hi,

Since the children of a form are stored in an array() in the FormBuilder, the order of fields when rendering of a form in the view using only form_widget(form) is determined by the way you added your fields in your FormType.

<?php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('field1', 'text')
        ->add('field2', 'text');

    // and so on...
}

The problem is : when using an EventSubscriber (like preSetData) in order to add or some fields in your form, you cannot choose where you want to add these fields.

<?php

public function preSetData(DataEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();

    if (null !== $data) {
        $form
            ->add($this->factory->createNamed('field3', 'text'))
            ->add($this->factory->createNamed('field4', 'text'));

        // and so on
    }
}

This example will render the form in this order :

  • field3
  • field4
  • field1
  • field2

In my case it's really annoying because I can't modify the view. The redering of my forms is generic and done only using form_widget(form).

So, is there any way to specify the order of the fields when using an EventSubscriber ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions