<p dir="auto">Hi,</p>
<p dir="auto">Since the children of a form are stored in an <code class="notranslate">array()</code> in the <code class="notranslate">FormBuilder</code>, the order of fields when rendering of a form in the view <strong>using only <code class="notranslate">form_widget(form)</code></strong> is determined by the way you added your fields in your <code class="notranslate">FormType</code>.</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;?php

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

    // and so on...
}"><pre class="notranslate"><span class="pl-ent">&lt;?php</span>

<span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">buildForm</span>(<span class="pl-smi"><span class="pl-smi">FormBuilderInterface</span></span> <span class="pl-s1"><span class="pl-c1">$</span>builder</span>, <span class="pl-smi">array</span> <span class="pl-s1"><span class="pl-c1">$</span>options</span>)
{
    <span class="pl-s1"><span class="pl-c1">$</span>builder</span>
        -&gt;<span class="pl-en">add</span>(<span class="pl-s">'field1'</span>, <span class="pl-s">'text'</span>)
        -&gt;<span class="pl-en">add</span>(<span class="pl-s">'field2'</span>, <span class="pl-s">'text'</span>);

    <span class="pl-c">// and so on...</span>
}</pre></div>
<p dir="auto"><strong>The problem is :</strong> when using an <code class="notranslate">EventSubscriber</code> (like <code class="notranslate">preSetData</code>) in order to add or some fields in your form, you cannot choose where you want to add these fields.</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;?php

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

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

        // and so on
    }
}"><pre class="notranslate"><span class="pl-ent">&lt;?php</span>

<span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">preSetData</span>(<span class="pl-smi"><span class="pl-smi">DataEvent</span></span> <span class="pl-s1"><span class="pl-c1">$</span>event</span>)
{
    <span class="pl-s1"><span class="pl-c1">$</span>data</span> = <span class="pl-s1"><span class="pl-c1">$</span>event</span>-&gt;<span class="pl-en">getData</span>();
    <span class="pl-s1"><span class="pl-c1">$</span>form</span> = <span class="pl-s1"><span class="pl-c1">$</span>event</span>-&gt;<span class="pl-en">getForm</span>();

    <span class="pl-k">if</span> (<span class="pl-c1">null</span> !== <span class="pl-s1"><span class="pl-c1">$</span>data</span>) {
        <span class="pl-s1"><span class="pl-c1">$</span>form</span>
            -&gt;<span class="pl-en">add</span>(<span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-c1">factory</span>-&gt;<span class="pl-en">createNamed</span>(<span class="pl-s">'field3'</span>, <span class="pl-s">'text'</span>))
            -&gt;<span class="pl-en">add</span>(<span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-c1">factory</span>-&gt;<span class="pl-en">createNamed</span>(<span class="pl-s">'field4'</span>, <span class="pl-s">'text'</span>));

        <span class="pl-c">// and so on</span>
    }
}</pre></div>
<p dir="auto">This example will render the form in this order :</p>
<ul dir="auto">
<li>field3</li>
<li>field4</li>
<li>field1</li>
<li>field2</li>
</ul>
<p dir="auto">In my case it's really annoying because I can't modify the view. The redering of my forms is generic and done <strong>only using <code class="notranslate">form_widget(form)</code></strong>.</p>
<p dir="auto">So, is there any way to specify the order of the fields when using an <code class="notranslate">EventSubscriber</code> ?</p>
<p dir="auto">Functionality request.</p>
<p dir="auto">It would be great if you could specify the order of the fields when using the following function.</p>
<p dir="auto">{{ form_rest(form) }}</p>
<p dir="auto">This would allow you to render all the fields without having to use the field_row etc.</p>
<p dir="auto">This would mean that if you had subscriber fields (automatically added to the bottom of the fields) you could specify the order in the form_rest.</p>