代码之家  ›  专栏  ›  技术社区  ›  Kristen Joseph-Delaffon

表单集合中的子项未提交

  •  0
  • Kristen Joseph-Delaffon  · 技术社区  · 6 年前

    我有一个带有集合字段的窗体。提交父表单时,不会提交集合字段中添加的子表单。

    以下是探查器所说的:

    enter image description here

    请求/响应选项卡:

    enter image description here

    我在Creneaux收藏中添加了一个孩子,但正如您所见,Creneaux中没有提交任何孩子。 不过,我在提交的表格中没有任何错误。

    我找不到我错在哪里了。

    父窗体:

    class MiseEnLigneFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('nom', TextType::class, ['label' => 'Titre'])
    
                    ->add('tempsEpreuve', TimeType::class,
                                                  ['label' => 'Durée',
                                                        'label_attr' => ['class' => 'active'],
                                                        'required' => true,
                                                        'widget' => 'single_text'])
    
                    ->add('creneaux', CollectionType::class,
                                                    ['label' => false,
                                                          'label_attr' => ['class' => 'active'],
                                                          'entry_type' => CreneauFormType::class,
                                                          'allow_add' => true,
                                                          'allow_delete' => true,
                                                          'by_reference' => false
                    ])
                ->add('polycopies', CollectionType::class,
                    ['label' => false,
                        'label_attr' => ['class' => 'active'],
                        'entry_type' => PolycopieFormType::class,
                        'allow_add' => true,
                        'allow_delete' => true,
                        'by_reference' => false
                    ]);
        }
    

    子窗体:

    class CreneauFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('dateDebut', DateTimeType::class, ['label' =>'Début',
                                                         'label_attr' => ['class'=>'active'],
                                                         'format' => 'dd-MM-yyyy HH:mm',
                                                         'widget' => 'single_text',
                                                         'attr' => ['data-field'=>'datetime']])
    
                    ->add('dateFin', DateTimeType::class, ['label' => 'Fin',
                                                       'label_attr' => ['class'=>'active'],
                                                       'format' => 'dd-MM-yyyy HH:mm',
                                                       'widget' => 'single_text',
                                                       'attr' => ['data-field'=>'datetime']])
    
                    ->add('test', CheckboxType::class, ['label' => false, 'required' => false]);
        }
    

    控制器:

    $originalCreneaux = new ArrayCollection();
    foreach ($colle->getCreneaux() as $creneau) {
        $originalCreneaux->add($creneau);
    }  
    
    $form = $this->createForm(MiseEnLigneFormType::class, $colle);
    $form->handleRequest($request);
    
    if ($form->isSubmitted()) {
            if ($form->isValid()) {
                return $this->render('Creneau/edit.html.twig', ['form'    => $form->createView(),
                                                    'colle' => $colle,
                                                    'matiereName' => $matiereName]);
    
            }
        }
    

    细枝:

    {{ form_start(form) }}
        <div class="card card-content">
           {{ form_widget(form.nom) }}
           {{ form_label(form.nom) }}
           {{ form_widget(form.tempsEpreuve) }}
           {{ form_label(form.tempsEpreuve) }}
        </div>
    
        <div class="card card-content">
            <table class="polycopies centered"
                       data-prototype="{{ form_widget(form.polycopies.vars.prototype)|e }}">
    
               <thead>
                  <tr>
                    <th>Nom</th>
                    <th>Supprimer</th>
                  </tr>
               </thead>
    
               <tbody class="poly_body">
                  {% for poly in form.polycopies %}
                     <tr>
                       <td>
                           {{ form_widget(poly.nom) }}
                           {{ form_label(poly.nom) }}
                       </td>
                       <td>
                           <a href="" class="delete_poly_link">
                              <i class="material-icons">delete</i>
                           </a>
                       </td>
                    </tr>
                 {% endfor %}
               </tbody>
            </table>
            <div class="" id="liensAjouterPolycopies">
               <a href="" class="add_poly_link btn-flat" data-tooltip="Ajouter un polycopié">
                 <i class="material-icons">add</i>Ajouter un polycopié
               </a>
        </div>
    
    <div class="card card-content">
        <span class="card-title">Créneaux</span>
    
        <table class="creneaux centered"
               data-prototype="{{ form_widget(form.creneaux.vars.prototype)|e }}">
    
            <thead>
            <tr>
                <th>Date de début</th>
                <th>Date de fin</th>
                <th>Test</th>
                <th>Supprimer</th>
            </tr>
            </thead>
    
            <tbody class="creneau_body">
            {% for creneau in form.creneaux %}
                <tr>
                    <td class="center-align">
                        {{ form_widget(creneau.dateDebut) }}
                        {{ form_label(creneau.dateDebut) }}
                    </td>
                    <td class="center-align">
                        {{ form_widget(creneau.dateFin) }}
                        {{ form_label(creneau.dateFin) }}
                    </td>
                    <td class="center-align">
                        {{ form_widget(creneau.test) }}
                        {{ form_label(creneau.test) }}
                    </td>
                    <td class="center-align">
                        <a href="" class="delete_creneau_link">
                            <i class="material-icons">delete</i>
                        </a>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
        {{ form_rest(form) }}
    </div>
    <button class="btn-large fullwidth" type="submit" value="Sauvegarder">
        <i class="material-icons left">save</i>
            Enregistrer
    </button>
    {{ form_end(form) }}
    
    <script>
        var collectionHolder = $('table.creneaux');
        var num = $('.creneau_body').children().length + 1;
    
        jQuery(document).ready(function () {
    
        $(document).on("click", "a.add_creneau_link", function (e) {
            e.preventDefault();
    
            addCreneauForm(collectionHolder);
            return false;
        });
    
        $(document).on("click","a.delete_creneau_link", function () {
            var $this = $(this);
    
            $this.closest('tr').remove();
    
            return false;
        });
    
        function addCreneauForm(collectionHolder) {
    
            var prototype = collectionHolder.attr('data-prototype');
            var newForm = prototype.replace(/__name__/g, num);
            num += 1;
    
            $tableau = newForm.split("<div>");
            var form = '';
            for (var i = 1; i < $tableau.length; i++) {
                form += "<td class='center-align'>" + $tableau[i] + "</td>";
            }
            form += "<td class='center-align'><a href='' class='delete_creneau_link'><i class='material-icons'>delete</i></a></td>";
    
            var $form = $('<tr></tr>').append(form);
            $form.insertAfter('.creneaux tr:last');
        }
    </script>
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Khadija    6 年前

    提交事件之后没有持久性或刷新 $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($creneau); $entityManager->flush(); 你不小心忘了吗?