title: How to create a Symfony form that has a nested form for each active user in the database?
body: <p>I am trying to create an admin page for a sports club website so that each month, the admin user can generate new invoices (<kbd>Invoice</kbd> entity) for all active members (<kbd>Member</kbd> entity) of the club. </p>
  
  <p>I'm trying to create the form so that I have one row for each member pre-populated with their standard monthly fee and the current date (both of which can be changed for individual member entries if needed):</p>
  
  <p><a href="https://i.stack.imgur.com/DMQzC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DMQzC.jpg" alt="Diagram of forms required"></a></p>
  
  <p>I have tried just about everything I can think of to get this working in a form but so far I've had no success. Below is the code as it currently stands but this gives me an individual form for just the last member....any advice on what I'm doing wrong would be very welcome - thanks in advance!</p>
  
  <p><strong>Entities (Member and Invoice):</strong></p>
  
  <pre><code>class Member
  {
      /**
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @ORM\OneToMany(targetEntity="Invoice", mappedBy="member_id")
       */
      protected $invoice_ids;
  
      /**
       * @ORM\COLUMN(type="string", length=100)
       * @Assert\NotBlank()
       */
      protected $firstname;
  
      /**
       * @ORM\COLUMN(type="string", length=100)
       */
      protected $familyname;
  
      /**
       * @ORM\COLUMN(type="boolean")
       */
      protected $active = true;
  
      /**
       * @ORM\COLUMN(type="decimal", precision=7, scale=2)
       * @Assert\Regex(
       *     pattern="/^\s*-?[1-9]\d*(\.\d{1,2})?\s*$/",
       *     match=true,
       *     message="Error")
       */
      protected $defaultinvoiceamount;
  
      public function __construct()
      {
          $this-&gt;invoice_ids        = new ArrayCollection();
      }
  
      public function FullName()
      {
          return $this-&gt;firstname . ' ' . $this-&gt;familyname;
      }
  }
  
  
  class Invoice
  {
      /**
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
      /**
       * @ORM\ManyToOne(targetEntity="Member", inversedBy="invoice_ids")
       * @ORM\JoinColumn(name="member_id",     referencedColumnName="id")
       */
      protected $member_id;
  
      /**
       * @ORM\COLUMN(type="decimal", precision=7, scale=2)
       * @Assert\Regex(
       *     pattern="/^\s*-?[1-9]\d*(\.\d{1,2})?\s*$/",
       *     match=true,
       *     message="Error")
       */
      protected $amount;
  
      /**
       * @ORM\COLUMN(type="datetime")
       * @Assert\DateTime()
       */
      protected $invoicedate;
  
  
      /**
       * @ORM\COLUMN(type="datetime")
       * @Assert\DateTime()
       */
      protected $createdate;
  
      /**
       * @ORM\COLUMN(type="text", nullable=True)
       */
      protected $comments;
  
  }
  </code></pre>
  
  <p><strong>FormType:</strong></p>
  
  <pre><code>class InvoiceType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('member_id', EntityType::class, array(
                      'class' =&gt; 'AppBundle:Member',
                      'choice_label' =&gt; 'FullName',
                      'attr' =&gt; array(
                           'readonly' =&gt; 'readonly'
                      )
                  )
              )
  
              -&gt;add('invoicedate', DateType::class, array(
                  'widget' =&gt; 'single_text',
                  'data' =&gt; new \DateTime('now'),
                  'format' =&gt; 'dd/MMM/yyyy',
                  'label' =&gt; 'Date of invoice',
              ))
  
              -&gt;add('createdate', DateType::class, array(
                  'widget' =&gt; 'single_text',
                  'data' =&gt; new \DateTime('now'),
                  'format' =&gt; 'dd/MMM/yyyy',
                  'label' =&gt; 'Date invoice recorded in database',
                  'disabled' =&gt; 'true'
              ))
  
              -&gt;add('amount', MoneyType::class, array(
                  'label' =&gt; 'Amount',
              ))
  
              -&gt;add('comments')
          ;
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\Invoice',
          ));
      }
  
      public function getName()
      {
          return 'invoice';
      }
  }
  </code></pre>
  
  <p><strong>Repository:</strong></p>
  
  <pre><code>class MemberRepository extends EntityRepository
  {
  
      /**
       * @return \Doctrine\ORM\QueryBuilder
       */
      public function findAllActiveMembers()
      {
  
          return $this-&gt;getEntityManager()
              -&gt;createQuery
              (
                  'SELECT m
                   FROM AppBundle:Member m
                   WHERE m.active= :active
                   ORDER BY m.surname, m.firstname'
              )
              -&gt;setParameter('active' ,true)
              -&gt;getResult();
      }
  }
  </code></pre>
  
  <p><strong>The Controller:</strong></p>
  
  <pre><code>/** 
   * @Route("/batchinvoices"  ,name="batchinvoices")
   */
  public function newBatchInvoicesAction(Request $request)
  {
      $members = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:Members')-&gt;findAllActiveMembers(); 
  
      foreach ($members as $member) {
          $invoices = new Invoice();
          $invoices-&gt;setMemberId($member);
          $form=$this-&gt;createForm(InvoiceType::class, $invoices);
      }
  
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted() &amp;&amp; ($form-&gt;isValid())) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($invoices);
          $em-&gt;flush();
          return $this-&gt;redirectToRoute('invoices_added');
      }
  
      return $this-&gt;render('admin/batchinvoices.html.twig', array(
          'form' =&gt; $form-&gt;createView(),
      ));
  }
  </code></pre>