Symfony form state backup to database and restore / or: how to prepopulate a form from database
<p>I have a big form with time, date, select and EntityType fields in it. The form is not linked to an entity, but it do contain fields from other entities. In other words I have no <code>data_class</code> in the OptionsResolver of the FormType.</p>

<p>Here is my formtype: (showing just one field for simplicity)</p>

<pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
{
        $builder
            -&gt;add('company', EntityType::class, array(
                'placeholder' =&gt; 'Companies',
                'class' =&gt; 'AppBundle:Company',
                'choice_label' =&gt; 'name',
                'multiple' =&gt; true,
                'query_builder' =&gt; function (EntityRepository $repository) {
                    return $repository-&gt;createQueryBuilder('c')-&gt;orderBy('c.name', 'ASC');
                },
                'required' =&gt; false
            ))
            //... much more fields
}

public function configureOptions(OptionsResolver $resolver)
{
    // This form is not linked to an Entity
    // Therefore no `data_class`
}
</code></pre>

<p>In the controller I can backup the data of a form. Called a FormState. I save a FormState to the database as follows:</p>

<pre><code>namespace AppBundle\Controller;

class ReportController extends Controller
{
    /** // ... */
    public function listAction(Request $request)
    {
        $form = $this-&gt;createForm(ReportType::class);

        $form-&gt;handleRequest($request);

        // Save the form to a FormState
        if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;getClickedButton()-&gt;getName() == 'saveFormState') {
            $formStateManager = $this-&gt;get('app.manager.form_state');

            // Get form data that we want to save
            $data = $form-&gt;getData();
            $name = 'Stackoverflow example'; // give it a name

            $formState = $formStateManager-&gt;createNewFormState(); // Create new FormState Entity object
            $formState-&gt;setName( $name );
            $formState-&gt;setData( $data );
            $formStateManager-&gt;save( $formState );
        }
        // ...
    }
}
</code></pre>

<p>All this above works perfect. But now the tricky part, I have to set the backupdata back to the form. The user can select a previous form state from a list of FormStates. And then hit the load button.</p>

<p>The <code>data</code> attribute of a FormState object I try to  load into the form is just a <code>$form-&gt;getData()</code> result. Thus a normal array without objects. Maybe that is the problem, but I can't get it to work whatsoever.</p>

<p>That <code>data</code> array is what I trying to load into the form so it takes its values over. I tried it via <code>$form-&gt;setData()</code>, or by creating a new form with <code>$this-&gt;createForm(ReportType::class, $data)</code>. Both fail with the same error message: <code>Entities passed to the choice field must be managed. Maybe persist them in the entity manager?</code>.</p>

<hr>

<p>I have tried two ways of adding the data:</p>

<p><strong>First try, in controller:</strong></p>

<pre><code>namespace AppBundle\Controller;

class ReportController extends Controller
{
    /** // ... */
    public function listAction(Request $request)
    {

        if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;getClickedButton()-&gt;getName() == 'loadFormState') {
            // ...

            $form = $this-&gt;createForm(ReportType::class, $formState-&gt;getData()); // &lt;-- throws the error
            $form-&gt;submit($formState-&gt;getData());
        }
    }

}
</code></pre>

<p><strong>Second try, via FormEvent subscriber:</strong>
When I do it via a FormEvent subscriber, like below, I get the same error. Here is my code:</p>

<pre><code>namespace AppBundle\Form\EventListener;

class ReportFieldsSubscriber implements EventSubscriberInterface
{

    // ...

    public static function getSubscribedEvents()
    {
        return array(
            FormEvents::PRE_SUBMIT =&gt; array(
                array('loadFormState'),
            ),
        );
    }

    /**
     * Load selected FormState in form
     * @param FormEvent $event
     */
    public function loadFormState(FormEvent $event)
    {
        $form = $event-&gt;getForm();

        //...   
        // Choosen FormState
        $formState = $formStateManager-&gt;findOneById($formStateId);

        $formStateData = $formState-&gt;getData();

        $form-&gt;setData($formStateData); // &lt;-- Set data, trows error
        // ...
    }
}
</code></pre>

<p>As I said both solutions throwing the error: <code>Entities passed to the choice field must be managed. Maybe persist them in the entity manager?</code></p>

<p>What is the preferred solution in this case? I can create a dummy Entity, and fix this problem, but it feels a bit ugly and not the way to go.</p>