Symfony2: Order form fields

Consider this piece of code:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text')
        ->add('description', 'textarea')
        ->add('status', 'choice', array('choices' => array('online' => 'online', 'offline' => 'offline')))
        ->add('save', 'submit');
}

But when I render this form I get it in the order

description
name
status

How can I change this besides writing the form template by hand.

Answers


You are hoping to control the render order from your PHP, but in reality, your PHP ought not know about, nor care about, how information is rendered. The order of rendering is handled in the template, preferably via twig, as it is the template's responsibility to handle this. It is a straightforward process, and gives you plenty of control. The following Twig template ought to fix your order:

{{form_start(nameOfFormVariablePassedFromController}}
    {{form_label(nameOfFormVariablePassedFromController.name)}}
    {{form_widget(nameOfFormVariablePassedFromController.name)}}

    {{form_label(nameOfFormVariablePassedFromController.description)}}
    {{form_widget(nameOfFormVariablePassedFromController.description)}}

    {{form_label(nameOfFormVariablePassedFromController.status)}}
    {{form_widget(nameOfFormVariablePassedFromController.status)}}

    {{form_widget(nameOfFormVariablePassedFromController.save)}}
{{form_end(nameOfFormVariablePassedFromController}}

It is easy to add classes to these labels and widgets for styling, by adding extra arguments. I can help with this if you need further clarification.


You can use https://github.com/egeloen/ivory-ordered-form/ bundle. Maybe someday it will be integrated into core. For now, symfony Form component doesn't have any order option.


Had same issue today with the form elements ordering.

Ended up with a trait that will override finishView method and reorder items in children property of a FormView:

trait OrderedTrait
{
    abstract function getFieldsOrder();

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        /** @var FormView[] $fields */
        $fields = [];
        foreach ($this->getFieldsOrder() as $field) {
            if ($view->offsetExists($field)) {
                $fields[$field] = $view->offsetGet($field);
                $view->offsetUnset($field);
            }
        }

        $view->children = $fields + $view->children;

        parent::finishView($view, $form, $options);
    }
}

Then in type implement getFieldsOrder method:

use OrderedTrait;

function getFieldsOrder()
{
    return [
        'first',
        'second',
        'next',
        'etc.',
    ];
}

Need Your Help

Bind a Swing GUI component's property to a custom class method (property)

java swing netbeans binding propertychangelistener

I'm working on my chat project. I've programmed server and client sides that works without GUI, just console UI. Now, while working on clients GUI (with Netbeans provided tools, not as I am used to...

How to reload view while user pinching UIScrollView?

ios uiview uiscrollview pinchzoom

I have view with CGPathRef and when user zooming this view by pinching view dont't reload:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.