title: Forms using mixed fields from various entities and rending with Twig in Symfony2
body: <h1>Problem</h1>
  
  <p>I have a Symfony2 2.3 project that consists of many entities (models). These models range from address, person, application and others amongst this. I needed a form to represent different steps in an online application. The application steps all contained many fields from various entities.</p>
  
  <p>So at first; I created matching FormTypes and Twig templates to match. Everything was fine until I needed to mix the forms up.</p>
  
  <p>For example, for one form, I needed:</p>
  
  <ul>
  <li>First name</li>
  <li>Surname</li>
  <li>DOB</li>
  <li>Postcode (from address entity)</li>
  <li>Application started date (from application entity)</li>
  </ul>
  
  <p>As you can see, the form consists of 2 extra elements from different forms.
  Thus I had the problem of binding the three layers together with use of Doctrine; entity, from type and twig template. (<a href="http://symfony.com/doc/current/book/forms.html#handling-form-submissions" rel="nofollow">http://symfony.com/doc/current/book/forms.html#handling-form-submissions</a>)</p>
  
  <h1>Current Solution</h1>
  
  <p>Thus so far, I have my models setup as they should be. So address details are within the address entity and personal details are stored in the person entity.</p>
  
  <p>I have then created all my FormTypes of how I want them to output to the screen, with the mixed fields in. Therefore, I have for example a Step-1 FormType that has:</p>
  
  <ul>
  <li>First name</li>
  <li>Second name</li>
  <li>DOB</li>
  <li>Address house number</li>
  <li>Address postcode</li>
  </ul>
  
  <p>Then in Twig simply output the fields. I was then going to add custom validation and custom methods to save the data to the DB, to the various tables. I came to the conclusion that I had to do this because none of the forms can map to a single entity.</p>
  
  <h1>What I Need</h1>
  
  <p>If anyone could shed some light on this and provide me with some examples, it would greatly appreciated!</p>
  
  <p>As you can see below, I have some fields that I have hard-coded that represent different properties from other entities and a FormType.</p>
  
  <ul>
  <li>I need a way to have all these entities in normal form.</li>
  <li>Have various FormType steps that have mixed properties from these various entities</li>
  <li>Some FormTypes will embed other FormTypes, e.g. address - as the user can add 3.</li>
  <li>Have Twig tempalte that will simply render each individual widget.</li>
  <li>A controller that loads the form, checks it is valid and saves values to their own tables.</li>
  </ul>
  
  <p>It may be, that I must create a totally custom method that saves the values individually but I was hoping for a 'Symfony way' of achieving this.</p>
  
  <h1>Example code</h1>
  
  <p>Assume all getters and setters created with Doctrine.
  Assume all Doctrine Mapping added with one-to-one relationships to link tables.</p>
  
  <p><strong>Person entity:</strong></p>
  
  <pre><code>private $id
  private $first_name
  private $last_name
  private $dob
  </code></pre>
  
  <p><strong>Address entity</strong></p>
  
  <pre><code>private $address_number
  private $address_postcode
  private $address_lived_years
  private $address_lived_months
  </code></pre>
  
  <p><strong>Application entity</strong></p>
  
  <pre><code>private $id
  private $plan
  </code></pre>
  
  <p><strong>AddresType</strong></p>
  
  <pre><code>$builder-&gt;add('address_number', 'text')
          -&gt;add('address_postcode', 'text')
  </code></pre>
  
  <p><strong>Example mixed form: Step1Type</strong></p>
  
  <pre><code>$builder-&gt;add('first_name', 'text')
          -&gt;add('last_name', text')
          -&gt;add('dob', 'date')
          -&gt;add('address_1', 'collection', array('type' =&gt; new AddressType()));
          -&gt;add('address_lived_years', 'text')
          -&gt;add('address_lived_months', 'text')
          -&gt;add('plan', 'text')
  </code></pre>
  
  <h1>Twig</h1>
  
  <pre><code>{{ form_start(step_1_form) }}
  
      {{ form_label(step_1_form.first_name, 'First Name') }}
      {{ form_errors(step_1_form.first_name) }}
      {{ form_widget(step_1_form.first_name) }}
  
      ... etc
  
  {{ form_end(step_1_form) }}
  </code></pre>
  
  <h1>Controller to bring it all together</h1>
  
  <pre><code>$step_1_form = $this-&gt;createForm(new StepOneType())-&gt;createView();
  
  return $this-&gt;render('MyBundle:Application:apply_step_1.html.twig', array(
                      'step_1_form' =&gt; $step_1_form));
  
  // Code needed to load form getForm() and handleRequest()
  </code></pre>