Symfony 3.4 Why CollectionType of formtype don’t post values

  doctrine-orm, php, symfony, symfony-3.4

I have a strange probleme with some rows of my form. I can’t add a new lines in my collectionType dates and my collectionType userIduser.

dates is a collectiontype of InterventionHasDate

userIduser is a collection type of Entity : user

When I add new rows with jquery, they are printed in HTML but when I post nothing is saved.
I tried to dump the values of my form but it’s like there is no new values in the form.
When I put manualy new dates in the database they are printed and I can modify or delete them in my page.
My form :

{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('idtypeinter',EntityType::class, [
            // looks for choices from this entity
            'class' => 'ssiplanificationBundle:Typeintervention',
            'label'=> 'Type de rendez-vous :',
            'choice_label' => 'nomtyperdv',
        ])
        ->add('Valider',SubmitType::class,['label'=>'Valider'])
        ->add('site',EntityType::class, [
            'class' => 'ssiplanificationBundle:Site',
            'choice_label' => 'nomSite',
            'label'=>'Site : ',
            'placeholder'=> 'Selectionnez votre site',
            'required'=>false,
            'mapped'=>false,
        ])->add('batiment',EntityType::class, [
            'class' => 'ssiplanificationBundle:Batiment',
            'choice_label' => 'nomBatiment',
            'label'=>'Batiment : ',
            'required'=>false,
            'mapped'=>false,
            'placeholder' => 'Veuillez choisir un batiment',
        ])->add('contactCorrespondant',EntityType::class, [
            'class' => 'ssiplanificationBundle:Contact',
            'choice_label' => 'nomContact',
            'label'=>'Contact à rencontrer sur site : ',
            'required'=>false,
        ]);


        $builder->add('installationIdinstallation',EntityType::class, [
            'class' => 'ssiplanificationBundle:Installation',
            'label'=> 'Installations :',
            'multiple' => true,
            'expanded' => true,
            'required'=> false,

        ])->add('visiteIdvisite',EntityType::class, [
           'class' => 'ssiplanificationBundle:Visite',
            'label'=> ' Visites :',
            'multiple' => true,
            'expanded' => true,
            'required'=> false,
        ])->add('AfficherplusInstallations',SubmitType::class,['label'=>'Afficher Plus'])

        ->add('AfficherplusVisites',SubmitType::class,['label'=>'Afficher Plus'])
        ->add('Validersite',SubmitType::class,['label'=>'Valider'])
        ->add('Validerbatiment',SubmitType::class,['label'=>'Valider'])
        ->add('numerointervention', TextType::Class,array('label'=> "Numéro d'intervention : ",'required'=>false))
        ->add('contactConfirmation',EntityType::class, [
             'class' => 'ssiplanificationBundle:Contact',
            'choice_label' => 'nomContact',
            'label'=>'Contact de confirmation par mail : ',
            'required'=>false,
        ])
        ->add('contactRdv',EntityType::class, [
            'class' => 'ssiplanificationBundle:Contact',
            'choice_label' => 'nomContact',
            'label'=>'Contact pour prise de rendez-vous téléphonique : ',
            'required'=>false,
        ])
        ->add('iduser',EntityType::class, [
            'class' => 'ssiplanificationBundle:User',
            'query_builder' => function (UserRepository $er) {
                return $er->createQueryBuilder('u')
                ->where('u.estTechnicien = :fonction')
                ->setParameter('fonction', 'Oui');
            },
            'choice_label' => 'username',
            'label'=>'Technicien : ',
        ])
         ->add('userIduser',CollectionType::class,[
            'entry_type' => EntityType::class,
            'entry_options' => [
                'class' => 'ssiplanificationBundle:User',
                'choice_label' => 'username',
                'label'=> false,
                'query_builder' => function (UserRepository $er) {
                    return $er->createQueryBuilder('u')
                    ->where('u.estTechnicien = :fonction')
                    ->setParameter('fonction', 'Oui');
                },
            ],
            'label'=>'Techniciens complémentaires :',
            'allow_add' => true,
            'allow_delete' => true,
        ])

        ->add('dates',CollectionType::class,[
            // looks for choices from this entity
            'entry_type' => InterventionHasDateType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'label'=>"Jours(s) d'intervention :",
        ])
        ->add('nomstatut',EntityType::class, [
            // looks for choices from this entity
            'class' => 'ssiplanificationBundle:Statut',
            // uses the User.username property as the visible option string
            'choice_label' => 'nomStatut',
            'label'=>'Statut : ',
        ])
        ->add('commentaire',TextareaType::class,['label'=>'Commentaire :','required'=>false])
        ->add('Enregistrer',SubmitType::class,['label'=>'Enregistrer'])
        ;

        $builder->get('site')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) {
                $form=$event->getForm();
                $this->addCorrespondantField($event->getForm()->getParent(),$event->getForm()->getData());
                $this->addBatimentField($event->getForm()->getParent(),$event->getForm()->getData());
            }
        );
        $builder->addEventListener(
            FormEvents::POST_SET_DATA,
            function (FormEvent $event) {
                $data= $event->getData();
                $installations= $data ->getInstallationIdinstallation(); 
                $visites= $data ->getVisiteIdvisite();
                if($data->getIdtypeinter()=="Vérification")
                {
                    if(! $installations->isEmpty())
                    {
                        $batiment=$installations[0]->getZoneIdzone()[0]->getIdbatiment();
                    }else{
                        $batiment=NULL;
                    }

                }else{
                    if(! $visites->isEmpty())
                    {

                        $batiment=$visites[0]->getInstallationIdinstallation()->getZoneIdzone()[0]->getIdbatiment();
                    }else{
                        $batiment=NULL;
                    }

                }
                if($batiment != NULL){
                     $site=$batiment->getIdsite();
                }else{
                    $batiment=NULL;
                    $site=NULL;
                }


                if($site !=NULL){

                    $this->addCorrespondantField($event->getForm(),$site);
                    $this->addBatimentField($event->getForm(),$site);
                    $this->addInstallationField($event->getForm(),$batiment);
                    $event->getForm()->get('site')->setData($site);
                    $event->getForm()->get('batiment')->setData($batiment);
                    $event->getForm()->get('installationIdinstallation')->setData($installations);
                    $event->getForm()->get('visiteIdvisite')->setData($visites);
                    $event->getForm()->get('contactCorrespondant')->setData($data ->getContactCorrespondant() );

                }else{
                    $this->addCorrespondantField($event->getForm(),$site);
                    $this->addBatimentField($event->getForm(),$site);
                    $this->addInstallationField($event->getForm(),$batiment);
                }
            }
        );


    }

My form for date :

class InterventionHasDateType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('dateintervention',DateType::class,array(
            'widget' => 'single_text',     
            'label'=> 'Le'
            )
        )
        ->add('heuredebut',TimeType::class,array('widget' => 'single_text',    
        'attr'=> ['step'=> '900' ] ,    
        'label'=> 'De'))
        ->add('heurefin',TimeType::class,array('widget' => 'single_text',  
        'attr'=> ['step'=> '900' ] ,      
        'label'=> 'A'))
        ;
    }

My controller:

public function PlanificationInterventionModifyAction(Intervention $intervention=NULL, Request $request)
    {
        if($intervention==NULL){
            $intervention=new Intervention();
        }
        $originalDates =array();
        // Create an ArrayCollection of the current Date objects in the database
        foreach ($intervention->getDates() as $inter) {
            $originalDates[]=$inter;
        }

        $originalTech= array();
        // Create an ArrayCollection of the current Date objects in the database
        foreach ($intervention->getUserIdUser() as $tech) {
            $originalTech[]=$tech;
        }
        $originalVisite= array();
        // Create an ArrayCollection of the current Date objects in the database
        foreach ($intervention->getVisiteIdvisite() as $visite) {
            $originalVisite[]=$visite;
        }

        $form = $this->createForm(InterventionType::class, $intervention);

        $form->handleRequest($request); 

        if ($form->get('Enregistrer')->isClicked() and  $form->isValid()) {

            $em = $this->getDoctrine()->getManager();
            foreach ($originalVisite as $visite) {
                if (false === $form->get("visiteIdvisite")->getData()->contains($visite)) {
                    // if you wanted to delete the Tag entirely, you can also do that
                    $visite->removeInterventionIdintervention($intervention);
                    $em->persist($visite);
                }
            }
            foreach ($form->get("visiteIdvisite")->getData() as $visite) {

                if (!in_array($visite,$originalVisite)) {
                    $visite->addInterventionIdintervention($intervention);
                    $em->persist($visite);
                }
            }
            foreach ($originalTech as $tech) {
                if (false === $form->get("userIduser")->getData()->contains($tech)) {
                    // if you wanted to delete the Tag entirely, you can also do that
                    $tech->removeInterventionIdintervention($intervention);
                    $em->persist($tech);
                }
            }
            foreach ($form->get("userIduser")->getData() as $tech) {

                if (!in_array($tech,$originalTech)) {
                    $tech->addInterventionIdintervention($intervention);
                    $em->persist($tech);
                }
            }
            dump($form->get("dates"));
            dump($form->get("dates")->getData());

            foreach ($originalDates as $date) {
                if (false === $form->get("dates")->getData()->contains($date)) {
                    // if you wanted to delete the Tag entirely, you can also do that
                    $em->remove($date);
                }
            }
            foreach ($form->get("dates")->getData() as $date) {

                if (!in_array($date,$originalDates)) {

                    $date->setInterventionIdintervention($intervention);
                    $em->persist($date);
                }
            }

            $em->persist($intervention);
            $em->flush();

            $request->getSession()->getFlashBag()->add('notice', 'Enregistrement effectué.');


            //return $this->redirectToRoute('ssiplanification_planification_homepage');
        }
        return $this->render('planification/InterventionNew.html.twig', array(
            'form' => $form->createView()));
    } 

The entity InterventionHasDate:

/**
 * InterventionHasDate
 *
 * @ORMTable(name="intervention_has_date", indexes={@ORMIndex(name="intervention_idIntervention", columns={"intervention_idIntervention"})}, 
 * uniqueConstraints={
 *   @ORMUniqueConstraint(
 *     columns={"dateintervention", "HeureDebut","HeureFin","intervention_idIntervention"}
 *   )
 * })
 * @ORMEntity(repositoryClass="ssiplanificationBundleRepositoryInterventionHasDateRepository")
 */

class InterventionHasDate
{
    /**
     *@AssertDate
     * @var string A "Y-m-d" formatted value
     *@ORMId
     * @ORMColumn(name="dateIntervention", type="string", nullable=false, options={"default"="0000-00-00"})
     * 
     */
    private $dateintervention = '0000-00-00';
     /**
     * @var Intervention
     *@ORMId
     * @ORMManyToOne(targetEntity="Intervention", inversedBy="dates")
     * 
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="intervention_idIntervention", referencedColumnName="idIntervention")
     * })
     */
    private $interventionIdintervention;

    /**
     * @var Time
     *
     * @ORMColumn(name="HeureDebut", type="time", nullable=false)
     */
    private $heuredebut;

    /**
     * @var Time
     *
     * @ORMColumn(name="HeureFin", type="time", nullable=false)
     */
    private $heurefin;

   /**
 * @return DateTime
 */

    public function getDateintervention()
    {

        return DateTime::createFromFormat('Y-m-d|', $this->dateintervention);
    }


    public function getHeuredebut(): ?DateTimeInterface
    {
        return $this->heuredebut;
    }

    public function setHeuredebut(DateTimeInterface $heuredebut): self
    {
        $this->heuredebut = $heuredebut;

        return $this;
    }

    public function getHeurefin(): ?DateTimeInterface
    {
        return $this->heurefin;
    }

    public function setHeurefin(DateTimeInterface $heurefin): self
    {
        $this->heurefin = $heurefin;

        return $this;
    }

    public function getInterventionIdintervention(): ?Intervention
    {
        return $this->interventionIdintervention;
    }

    public function setInterventionIdintervention(?Intervention $interventionIdintervention): self
    {
        $this->interventionIdintervention = $interventionIdintervention;

        return $this;
    }



    /**
     * Set dateintervention.
     *
     * @param DateTime $dateintervention
     *
     * @return InterventionHasDate
     */
    public function setDateintervention($dateintervention)
    {
        $this->dateintervention = $dateintervention->format('Y-m-d');

        return $this;
    }

}

The entity intervention :

/**
 * Intervention
 *
 * @ORMTable(name="intervention", uniqueConstraints={@ORMUniqueConstraint(name="idIntervention", columns={"idIntervention"})}, indexes={@ORMIndex(name="contact_rdv_2", columns={"contact_rdv"}), @ORMIndex(name="contact_confirmation", columns={"contact_confirmation"}), @ORMIndex(name="idUser", columns={"idUser"}), @ORMIndex(name="contact_correspondant", columns={"contact_correspondant"}), @ORMIndex(name="nomStatut", columns={"nomStatut"})})
 * @ORMEntity(repositoryClass="ssiplanificationBundleRepositoryInterventionRepository")
 */
class Intervention
{
    /**
     * @var int
     *
     * @ORMColumn(name="idIntervention", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $idintervention;
     .....


    /**
     * @var DoctrineCommonCollectionsCollection
     *@ORMOneToMany(targetEntity="InterventionHasDate", mappedBy="interventionIdintervention")
     */
    private $dates;



    /**
     * @var DoctrineCommonCollectionsCollection
     *
     * @ORMManyToMany(targetEntity="User", inversedBy="interventionIdintervention")
     * @ORMJoinTable(name="intervention_has_user",
     *   joinColumns={
     *     @ORMJoinColumn(name="intervention_idIntervention", referencedColumnName="idIntervention")
     *   },
     *   inverseJoinColumns={
     *     @ORMJoinColumn(name="user_idUser", referencedColumnName="idUser")
     *   }
     * )
     */
    private $userIduser;

    /**
     * @var DoctrineCommonCollectionsCollection
     *
     * @ORMManyToMany(targetEntity="Visite", mappedBy="interventionIdintervention")
     */
    private $visiteIdvisite;

    /**
     * @return Collection|InterventionHasDate[]
     */
    public function getDates(): Collection
    {
        return $this->dates;
    }

    public function addDates(InterventionHasDate $dates): self
    {
        if (!$this->dates->contains($dates)) {
            $this->dates[] = $dates;
            $dates->addDates($this);
        }

        return $this;
    }

    public function removeDates(InterventionHasDate $dates): self
    {
        if ($this->dates->contains($dates)) {
            $this->dates->removeElement($dates);
            $dates->removeDates($this);
        }

        return $this;
    }


    /**
     * @return Collection|User[]
     */
    public function getUserIduser(): Collection
    {
        return $this->userIduser;
    }

    public function addUserIduser(User $userIduser): self
    {
        if (!$this->userIduser->contains($userIduser)) {
            $this->userIduser[] = $userIduser;
            $userIduser->addInterventionIdintervention($this);
        }

        return $this;
    }

    public function removeUserIduser(User $userIduser): self
    {
        if ($this->userIduser->contains($userIduser)) {
            $this->userIduser->removeElement($userIduser);
            $userIduser->removeInterventionIdintervention($this);
        }

        return $this;
    }


    public function __construct()
    {
        $this->installationIdinstallation = new DoctrineCommonCollectionsArrayCollection();
        $this->userIduser = new DoctrineCommonCollectionsArrayCollection();
        $this->visiteIdvisite = new DoctrineCommonCollectionsArrayCollection();
        $this->dates=new DoctrineCommonCollectionsArrayCollection();
    }

My html/twig :

{% extends 'planification/base.html.twig' %}
{% block title %}
Home
{% endblock %}
{% block body %}
 <div id="corps">

        <h1>Intervention</h1>
        <h3>{{ form_start(form)}}</h3>
        {{ form_errors(form)}}

        <div class="block-position">
        {{ form_errors(form.idtypeinter) }}
        <h3>{{ form_label(form.idtypeinter)}}{{ form_widget(form.idtypeinter)}}{{ form_widget(form.Valider) }}</h3>
        </div>

        {% if form.idtypeinter.vars.value == "Dépannage" 
        or form.idtypeinter.vars.value == "Mise en service" 
        or form.idtypeinter.vars.value == "Travaux" 
        or form.idtypeinter.vars.value == "Vérification" %}
    <div class="block-position">

            {{ form_errors(form.site) }}
            <h3>{{ form_label(form.site) }}{{ form_widget(form.site) }}{{ form_widget(form.Validersite) }}</h3>
            {{ form_errors(form.batiment) }}
            <h3>{{ form_label(form.batiment) }}{{ form_widget(form.batiment) }}{{ form_widget(form.Validerbatiment) }}</h3>

            {% if form.idtypeinter.vars.value != "Vérification" %}
                <h3>{{ form_errors(form.installationIdinstallation) }}{{ form_label(form.installationIdinstallation) }}</h3>
                {% if form.batiment.vars.value != NULL %}
                    {% for installation in form.installationIdinstallation %}
                        <h4>{{ form_widget(installation) }}{{ form_label(installation) }}</h4>
                    {% endfor %}
                    <h3>{{ form_row(form.AfficherplusInstallations) }}</h3>
                {% endif %}
            {% else %}
                <h3>{{ form_errors(form.visiteIdvisite) }}{{ form_label(form.visiteIdvisite) }}</h3>
                {% if form.batiment.vars.value != NULL %}
                    {% for visite in form.visiteIdvisite %}
                        <h4>{{ form_widget(visite) }}{{ form_label(visite) }}</h4>
                    {% endfor %}
                    <h3>{{ form_row(form.AfficherplusVisites) }}</h3>
                {% endif %}
            {% endif %}
            </div>
        <div class="block-position">
        {{ form_errors(form.numerointervention) }}
        <h3>{{ form_row(form.numerointervention) }}</h3>
        {{ form_errors(form.contactCorrespondant) }}
        <h3>{{ form_row(form.contactCorrespondant) }}</h3>
        {{ form_errors(form.contactRdv) }}
        <h3>{{ form_row(form.contactRdv) }}</h3>
        {{ form_errors(form.contactConfirmation) }}
        <h3>{{ form_row(form.contactConfirmation) }}</h3>
        </div>
        {% endif %}
        <div class="block-position">
        {{ form_errors(form.dates) }}
        <h3>{{ form_label(form.dates) }}
        <ul id="dates-fields-list" 
        data-prototype="{{form_widget(form.dates.vars.prototype) |e('html_attr')}}"
        data-widget-tags="{{ '<li></li>'|e }}"
        data-widget-counter="{{ form.dates|length }}">
            {% for datesField in form.dates %}
                <li id="{{ "list_" ~ datesField.vars.id }}">
                    {{ form_widget(datesField.dateintervention)}}
                    {% if form.idtypeinter.vars.value != "Astreinte" %}
                    {{form_widget(datesField.heuredebut ) }}

                    {{form_widget(datesField.heurefin  )}}
                    {% endif %}
                    <button  type="button" class="remove-another-collection-widget" onclick="deleteLink( {{ "list_" ~ datesField.vars.id }})">Retirer</button>

                </li>

        {% endfor %}

        </ul><button type="button" class="add-another-collection-widget" data-list-selector="#dates-fields-list">Ajouter</button></h3>
        </div>
        <div class="block-position">
        {{ form_errors(form.iduser) }}
        <h3>{{ form_row(form.iduser) }}</h3>
        {{ form_errors(form.commentaire) }}
        <h3>{{ form_row(form.commentaire) }}</h3>
        {{ form_errors(form.nomstatut) }}
        <h3>{{ form_row(form.nomstatut) }}</h3>
        </div>
        <div class="block-position">
        {{ form_errors(form.userIduser) }}
        <h3>{{ form_label(form.userIduser) }}

        <ul id="technicien-fields-list" data-prototype="{{ form_widget(form.userIduser.vars.prototype)|e('html_attr') }}"
        data-widget-tags="{{ '<li></li>'|e }}"
        data-widget-counter="{{ form.userIduser|length }}">
            {% for technicienField in form.userIduser %}
                <li id="{{ "list_" ~ technicienField.vars.id }}">
                    {{ form_errors(technicienField) }}
                    {{ form_widget(technicienField) }} <button  type="button" class="remove-another-collection-widget" onclick="deleteLink( {{ "list_" ~ technicienField.vars.id }})">Retirer</button>

                </li>

        {% endfor %}
        </ul> <button type="button" class="add-another-collection-widget" data-list-selector="#technicien-fields-list">Ajouter</button>
        </h3>
        <button type="submit">Essayer</button>
        </div>
        <h3>{{ form_row(form.Enregistrer) }}</h3>
        {{ form_row(form._token) }}
        {{ form_end(form, {'render_rest': false})}} 
</div>
{% endblock %}

The script :

jQuery(document).ready(function () {

    jQuery('.add-another-collection-widget').click(function (e) {
        var list = jQuery(jQuery(this).attr('data-list-selector'));
        // Try to find the counter of the list or use the length of the list
        var counter = list.data('widget-counter') || list.children().length;

        // grab the prototype template
        var newWidget = list.attr('data-prototype');
        // replace the "__name__" used in the id and name of the prototype
        // with a number that's unique to your emails
        // end name attribute looks like name="contact[emails][2]"
        newWidget = newWidget.replace(/__name__/g, counter);
        // Increase the counter
        counter++;
        // And store it, the length cannot be used if deleting widgets is allowed
        list.data('widget-counter', counter);

        // create a new list element and add it to the list
        var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
        addTagFormDeleteLink(newElem);
        newElem.appendTo(list);
    });
    function addTagFormDeleteLink($tagFormLi) {
        var $removeFormButton = $(' <button type="button"> Retirer</button>');
        $tagFormLi.append($removeFormButton);

        $removeFormButton.on('click', function(e) {
            // remove the li for the contact form
            $tagFormLi.remove();
        });
    }
});
function deleteLink(id){
    id.remove();
}

The script is working for the other pages of my site.
I am in trouble since some days already… If someone can help me I will be very gratefull.

Source: Symfony Questions

LEAVE A COMMENT