symfony3 Collection of Forms add array prob

class Itineraire
{

    /**
     * @ORMGeneratedValue
     * @ORMId
     * @ORMColumn(type="integer")
     */


    private $id ;
    /**
     * @ORMManyToOne(targetEntity="Croisiere")
     */

    public $croisiere;




    /**
     * @ORMColumn(type="integer",nullable=true)
     */
    private $jour ;
    /**
     * @ORMColumn(type="string",length=255,nullable=true)
     */

class Croisiere
{

     /**
     * @ORMGeneratedValue
     * @ORMId
     * @ORMColumn(type="integer")
     */

    private $id ;

    /**
 * @ORMManyToOne(targetEntity="Navire",inversedBy="Croisiere")
 * @ORMJoinColumn(name="id_navire", referencedColumnName="id")
 */

    public $id_navire;


 /**
     * @var ArrayCollection
     * @ORMOneToMany(targetEntity="Itineraire", mappedBy="Croisiere" )
     * @ORMOrderBy({"id"="ASC"})
     */

    protected $days ;


    formType = 
       ->add('days',CollectionType::class, array('entry_type' =>                    ItineraireType::class,
                'allow_add'    => true,
                'by_reference' => false,
                "prototype"=>true,
                "prototype_data"=>new Itineraire()

Action

public function addCruiseAction(Request $request)
{
    $user = $this->getUser();
    if (!is_object($user) ) {

        return $this->redirectToRoute('fos_user_security_login');
    }
    $id =  $this->getUser()->getId();

    $croisiere = new Croisiere();
    $em=$this->getDoctrine()->getManager();
    $ProfileCroisiere = $em->getRepository('GstaycarBundle:ProfileCroisiere')->findOneBy(array('id_user' => $id ));



    $tab = new ArrayCollection();

    // Create an ArrayCollection of the current Tag objects in the database




    /*$day1 = new Itineraire();

    $croisiere->getDays()->add($day1);
    $day2 = new Itineraire();

    $croisiere->getDays()->add($day2);*/


    $form = $this->createForm(CroisiereType::class,$croisiere);


    $form->handleRequest($request);

    if($form->isSubmitted() ) {
        if ($form->isValid()){
            var_dump($croisiere);
            $croisiere->setIdProfile($ProfileCroisiere);

            $em->persist($croisiere);

             foreach ($tab as $day) {


            $em->persist( $day);

            }

        //$em->persist( $day2);
        //$em->persist( $day1);
        $em->flush();
        //return $this->redirect($this->generateUrl('show_ship',array('msg'=>"add successful")));
        }
    }

   **twig** 

      <ul class="days" data-prototype="{{            form_widget(form.days.vars.prototype)|e }}">
        {% for f in form.days %}
            {{ form_widget(f) }}
        {% endfor %}
      </ul>
    </div>

Script Jquery

  var $collectionHolder;


   // setup an "add a tag" link
    var $addTagLink = $('');
   var $newLinkLi = $('
  • ').append($addTagLink); var index=0; jQuery(document).ready(function() { // Get the ul that holds the collection of days $collectionHolder = $('ul.days'); // add the "add a tag" anchor and li to the days ul $collectionHolder.append($newLinkLi); // count the current form inputs we have (e.g. 2), use that as the new // index when inserting a new item (e.g. 2) $collectionHolder.data('index', $collectionHolder.find(':input').length); $addTagLink.on('click', function(e) { // prevent the link from creating a "#" on the URL e.preventDefault(); // add a new tag form (see next code block) addTagForm($collectionHolder, $newLinkLi); }); }); function addTagForm($collectionHolder, $newLinkLi) { // Get the data-prototype explained earlier var prototype = $collectionHolder.data('prototype'); // get the new index var index = $collectionHolder.data('index'); // Replace '__name__' in the prototype's HTML to // instead be a number based on how many items we have var newForm = prototype.replace(/__name__/g, index); // increase the index with one for the next item $collectionHolder.data('index', index + 1); // Display the form in the page in an li, before the "Add a tag" link li var $newFormLi = $('
  • ').append(newForm); $newLinkLi.before($newFormLi); } <script> var $collectionHolder; // setup an "add a tag" link var $addTagLink = $('<button href="#" class="add_tag_link">Add a tag</button>'); var $newLinkLi = $('<li></li>').append($addTagLink); var index=0; jQuery(document).ready(function() { // Get the ul that holds the collection of days $collectionHolder = $('ul.days'); // add the "add a tag" anchor and li to the days ul $collectionHolder.append($newLinkLi); // count the current form inputs we have (e.g. 2), use that as the new // index when inserting a new item (e.g. 2) $collectionHolder.data('index', $collectionHolder.find(':input').length); $addTagLink.on('click', function(e) { // prevent the link from creating a "#" on the URL e.preventDefault(); // add a new tag form (see next code block) addTagForm($collectionHolder, $newLinkLi); }); }); function addTagForm($collectionHolder, $newLinkLi) { // Get the data-prototype explained earlier var prototype = $collectionHolder.data('prototype'); // get the new index var index = $collectionHolder.data('index'); // Replace '__name__' in the prototype's HTML to // instead be a number based on how many items we have var newForm = prototype.replace(/__name__/g, index); // increase the index with one for the next item $collectionHolder.data('index', index + 1); // Display the form in the page in an li, before the "Add a tag" link li var $newFormLi = $('<li></li>').append(newForm); $newLinkLi.before($newFormLi); }

  • Source: stackoverflow-php