title: Symfony2 Custom Form. Miss ideas
body: <p>I have two entities as follows:</p>
  
  <p><strong>Member</strong> (name, firstname) : OneToMany -> subscriptions</p>
  
  <p><strong>Subscription</strong> ( numberOfMounth ) : ( ManyToOne -> member)</p>
  
  <p>I'd like to create a form that will load the list of members.
  And for each member, I can choose the number of subscription months (numberOfMounth).
  With a check box before each member to select only those who are ready for subscription . And finally a button to record everything .</p>
  
  <p>I started by creating a form by adding a field collection ( subscriptions ) .
  But I wonder whether to create a new entity for this form.</p>
  
  <p>Actually I miss ideas.</p>
  
  <p>My entities and form:</p>
  
  <pre><code>&lt;?php
  
  namespace Jack\AikidoBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Jack\AikidoBundle\Member
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Jack\AikidoBundle\Entity\MemberRepository")
   * @ORM\HasLifecycleCallbacks()
   */
  class Member
  {
  
      /**
      * @var integer $id
      *
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
      private $id;
  
      /**
      * @var string $name
      *
      * @ORM\Column(name="member_name", type="string", length=255)
      */
      private $name;
  
      /**
      * @var string $firstname
      *
      * @ORM\Column(name="member_firstname", type="string", length=255)
      */
      private $firstname;
  
  
      /**
       * @ORM\OneToMany(targetEntity="Jack\AikidoBundle\Entity\Subscription", mappedBy="member", cascade={"persist", "remove"})
       */
      protected $subscriptions;
  
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;subscriptions = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Member
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set firstname
       *
       * @param string $firstname
       * @return Member
       */
      public function setFirstname($firstname)
      {
          $this-&gt;firstname = $firstname;
  
          return $this;
      }
  
      /**
       * Get firstname
       *
       * @return string 
       */
      public function getFirstname()
      {
          return $this-&gt;firstname;
      }
  
  
  
      /**
       * Add subscriptions
       *
       * @param \Jack\AikidoBundle\Entity\Subscription $subscriptions
       * @return Member
       */
      public function addSubscription(\Jack\AikidoBundle\Entity\Subscription $subscriptions)
      {
          $this-&gt;subscriptions[] = $subscriptions;
  
          return $this;
      }
  
      /**
       * Remove subscriptions
       *
       * @param \Jack\AikidoBundle\Entity\Subscription $subscriptions
       */
      public function removeSubscription(\Jack\AikidoBundle\Entity\Subscription $subscriptions)
      {
          $this-&gt;subscriptions-&gt;removeElement($subscriptions);
      }
  
      /**
       * Get subscriptions
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getSubscriptions()
      {
          return $this-&gt;subscriptions;
      }
  }
  
  &lt;?php
  
  namespace Jack\AikidoBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Jack\AikidoBundle\Subscription
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Jack\AikidoBundle\Entity\SubscriptionRepository")
   * @ORM\HasLifecycleCallbacks()
   */
  class Subscription
  {
      /**
       * @ORM\ManyToOne(targetEntity="Jack\AikidoBundle\Entity\Member", inversedBy="subscriptions")
       * @ORM\JoinColumn(name="member_id", referencedColumnName="id")
       */
      protected $member;
     /**
      * @var integer $id
      *
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
      private $id;
  
  
      /**
      * @var datetime $date
      *
      * @ORM\Column(name="date", type="datetime")
      */
        private $date;
  
      /**
      * @var integer $nbMonth
      *
      * @ORM\Column(name="nbMonth", type="integer")
      */
      private $nbMonth;
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
  
  
      /**
       * Set date
       *
       * @param \DateTime $date
       * @return Subscription
       */
      public function setDate($date)
      {
          $this-&gt;date = $date;
  
          return $this;
      }
  
      /**
       * Get date
       *
       * @return \DateTime 
       */
      public function getDate()
      {
          return $this-&gt;date;
      }
  
      /**
       * Set nbMonth
       *
       * @param integer $nbMonth
       * @return Subscription
       */
      public function setNbMonth($nbMonth)
      {
          $this-&gt;nbMonth = $nbMonth;
  
          return $this;
      }
  
      /**
       * Get nbMonth
       *
       * @return integer 
       */
      public function getNbMonth()
      {
          return $this-&gt;nbMonth;
      }
  
      /**
       * Set member
       *
       * @param \Jack\AikidoBundle\Entity\Member $member
       * @return Subscription
       */
      public function setMember(\Jack\AikidoBundle\Entity\Member $member)
      {
          $this-&gt;member = $member;
  
          return $this;
      }
  
      /**
       * Get member
       *
       * @return \Jack\AikidoBundle\Entity\Member 
       */
      public function getMember()
      {
          return $this-&gt;member;
      }
  
  
  }
  
  &lt;?php
  
  namespace Jack\AikidoBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class SubscriptionType extends AbstractType
  {
          /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('date')
              -&gt;add('nbMonth')
  
              -&gt;add('member', 'entity', array(
                  'class' =&gt; 'JackAikidoBundle:Member',
                  'property' =&gt; 'name',
                  'multiple' =&gt; true,
                  'expanded' =&gt; true,
              ))                 
          ;
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Jack\AikidoBundle\Entity\Subscription'
          ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'subscription';
      }
  }
  </code></pre>
  
  <p><strong>New Update:</strong> My controller and view</p>
  
  <pre><code>public function subscriptionAction()
  {    
      $entity = new Subscription();
  
      $repository = $this-&gt;getDoctrine()
                         -&gt;getRepository('JackAikidoBundle:Member');
  
      $members = $repository-&gt;findAll();
  
      $formBuilderPratSub = $this-&gt;createFormBuilder();
  
      //Make a loop for each $member
      foreach($members as $member)
      {
          $formBuilder = $this-&gt;get('form.factory')-&gt;createNamedBuilder(
                  $member-&gt;getId(), 
                  new SubscriptionType(), 
                  $entity
                  );
          $formBuilderPratSub-&gt;add($formBuilder);
      }
      //Create the form
  
      $form = $formBuilderPratSub-&gt;getForm();
      $form-&gt;add('submit', 'submit', array('label' =&gt; 'Create'));
  
      return $this-&gt;render('JackAikidoBundle:Aikido:souscription.html.twig', 
          array(
              'entity' =&gt; $entity,
              'forms'   =&gt; $form-&gt;createView(),
              'members' =&gt; $members,
      ));
  
  }
  </code></pre>
  
  <p>And my view:</p>
  
  <pre><code>    &lt;table class="table"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;Member ID&lt;/th&gt;
                  &lt;th&gt;Name&lt;/th&gt;
                  &lt;th&gt;Firstname&lt;/th&gt;
                  &lt;th&gt;Nb Month&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              {% for member in members %}
                  {% for form in forms %}
                      &lt;tr&gt;
                          &lt;td&gt;{{ member.id }}&lt;/td&gt;
                          &lt;td&gt;{{ member.nom }}&lt;/td&gt;
                          &lt;td&gt;{{ member.prenom }}&lt;/td&gt;
                          &lt;td&gt;{{ form_row(form.nbMonth)}}&lt;/td&gt;
                      &lt;/tr&gt;
  
                  {% endfor %}
  
              {% endfor %}
          &lt;/tbody&gt;
      &lt;/table&gt;
     &lt;div&gt;{{ form_row(forms.submit) }}&lt;/div&gt; 
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Method "nbMonth" for object "Symfony\Component\Form\FormView" does not exist in JackAikidoBundle:Aikido:souscription.html.twig at line 25 
  </code></pre>
  
  <p>This line is: <code>&lt;td&gt;{{ form_row(form.nbMonth)}}&lt;/td&gt;</code></p>
  
  <p>I try: <code>{{ form(forms)}}</code> to display just the form:</p>
  
  <p><a href="https://i.stack.imgur.com/0aGAW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0aGAW.png" alt="form display"></a></p>