In our Symfony2 project we have a very complex structure for forms with embedded forms.... Now we got the requirement to bring the output of the form in an specific order.
And here is the problem: We use the form_widget(form) and now we are looking for a solution in the object (e.g. via annotations) or the formbuilder to move a specific field to the end of the form. in symfony 1.4 it was the widget-movefield() function, i guess...
Thx...
As I understand, you want to use only form_widget(form) in final template.
Let assume we have two inherited models (ModelA, ModelB) and form types for them (ModelAType, ModelBType)
If you render formB, you will get A,B,C order, but you want A,C,B. To accomplish that, create form template and add reference to app. config file:
Now, when you render formB, you will see desired order and keep code structured. It happens because every widget rendered only once, so if you render it before calling parent block, you will change their order.
I had the same problem, but solved it in a different way. Here is my solution, as an idea for others who are searching for this problem.
You could add all the form fields in an event listener, because here you have all the objects data to decide on the fields order. You could for example use a method from the data object to decide on the fields order:
You can do it right in your controller before you render the template:
uasort
works as well, howeverusort
creates a non-associative array which will break the form rendering later on.You can re-order the fields using this bundle: https://github.com/egeloen/IvoryOrderedFormBundle
This allows you to do things like this:
This was going to be in core, but was rejected and pulled out into a bundle.
here is the solution I came up with.
I created a class that my types extend.
Then you can use it in your inherited classes.
This class extends the one above