title: Fill doctrine form with dynamic form elements
body: <p>I'm currently working on a Symfony2 project and I'm stuck with the form creation. I have the following setup:</p>
  
  <pre><code>&lt;?php
  class Field {
  private $name;
  private $label;
  private $type;
  /**
   * @ORM\OneToMany(targetEntity="Option", mappedBy="field")
   */
  private $options;
  
  ....
  ?&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;?php
  class Option {
  private $value;
  private $type;
  /**
   * @var string
   * 
   * @ORM\ManyToOne(targetEntity="Field", inversedBy="options")
   * @ORM\JoinColumn(name="field", referencedColumnName="name")
   */
  private $field;
  
  ....
  ?&gt;
  </code></pre>
  
  <p>Now, what I'd like to do, I'm creating a form, with the 'field' as a dropdown/option list/radiobutton/checkbox group (which is specified by the Field::$type) and fill the field with it's options. Any ideas?</p>
  
  <p>Edit: What could be important: I don't need the ability to add, remove or change the options. They are fix in the database and just need to be chosen from. Some with the mulitple, some without.</p>
  
  <p>Edit2: Just to clarify: I'm building my forms with Symfony Forms: I have a basic form type ("Case") which contains the Fields which contain its Options:</p>
  
  <pre><code>class CaseType extends AbstractType
  {
      private $case;
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $this-&gt;case = $options['data'];
          $builder
              -&gt;add('user')
              -&gt;add('fields', 'collection', array('type' =&gt; new FieldType()))
          ;
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>class FieldType extends AbstractType
  {
  
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
  
          $builder-&gt;add('options', 'entity', array(
                      "class"     =&gt; "MyBundle:Option",
                      "property"  =&gt; "value",
                      "query_builder" =&gt; function (EntityRepository $er) {
                          return $er-&gt;createQueryBuilder('o', 'Option')
                              -&gt;where('o.field = :f')
                              -&gt;setParameter('f', **type_of_current_field**)
                              -&gt;orderBy('o.value', "ASC");
                      }
          ));
  </code></pre>
  
  <p>Just this does not work as expected... First because I cannot access the field->type and second it's quite a mess</p>
  
  <p>Edit3: Okay, I absolutely don't get why this does not work:</p>
  
  <pre><code>class FieldType extends AbstractType
  {
  
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
  
          $formFactory = $builder-&gt;getFormFactory();
          $builder-&gt;addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formFactory) {
              $form = $event-&gt;getForm();
              $data = $event-&gt;getData();
  
              $form-&gt;add('options', 'entity', array(
                      "class"     =&gt; "MyBundle:Option",
                      "property"  =&gt; "value",
                      "multiple"  =&gt; "true",
              ));
          });
  </code></pre>
  
  <p>...
  ?></p>
  
  <p>This just gives my the following error:
      Warning: Invalid argument supplied for foreach() </p>
  
  <p>Shouldn't I be able to just parse out all of the options for one field? Or is my approach completely wrong and I should do it different? When I dump out my fields it looks like this:</p>
  
  <pre><code>Field {#427 ▼
    -id: 26
    -name: "MyField"
    -label: "field to fill out"
    -type: "D"
    -public: 1
    -options: PersistentCollection {#440 ▼
      -snapshot: array:2 [ …2]
      -owner: Field {#427}
      -association: array:15 [ …15]
      -em: EntityManager {#289 …10}
      -backRefFieldName: "field"
      -typeClass: ClassMetadata {#401 …}
      -isDirty: false
      -initialized: true
      -coll: ArrayCollection {#397 ▼
        -_elements: array:2 [▼
          0 =&gt; Option {#435 ▼
            -id: 91
            -value: 1
            -field: Field {#427}
            -type: "D"
          }
          1 =&gt; Option {#432 ▼
            -id: 92
            -value: 2
            -field: Field {#427}
            -type: "D"
          }
        ]
      }
    }
  }
  </code></pre>