Symfony:如何在foreach循环中动态创建表单

时间:2017-03-24 22:49:58

标签: symfony

我有一个包含隐藏字段的表单,它使用此字段向操作发送一个变量,该变量指示要删除的项的ID。我有一个表显示数据库表'profil'的内容,每行都有一个按钮“supprimer”删除这一行,但正如我所说,它只删除第一行。 Apparentlu这个循环只为第一个项创建一个表单,所以我想为表中的每个项创建这个表单。

 {% if profils %}
           <div id="prof_table">
            <table style="background-color: #f9f9f9;position: absolute;top:10vmin;left: 5vmin;"
                   class="table table-responsive table-bordered">
                <tr>
                    <th style="color: #1b6d85"> Identificateur </th>
                    <th style="color: #1b6d85"> Libellé</th>
                    <th style="color: #1b6d85"> Actions</th>
                </tr>
                {% for profil in profils %}
                <tr><td>{{ profil.id }}</td><td>{{ profil.libelle }}</td>
                    <td>
                        <button id="delete" class="btn btn-danger btn-xs">
                            <a data-fancybox data-src="#hidden-content3" href="javascript:;"
                               style="color: #ffffee;text-decoration: none;">Supprimer</a>
                        </button>
                    </td></tr>

                    <!-- HERE IS THE POPUP FORM TO DELETE PROFILES-->
                    <div style="display: none; height: 40vh; width: 30vw;" id="hidden-content3">
                        <span style="position:relative;top:10vmin;font-size: medium;"></span>
                    {% if form3 %}
                            <label style="display:inline-table;">
                                <span style="font-size: medium;">Etes vous surs de vouloir supprimer ce profil?</span>
                            </label>
                        </section>
                        {{ form_start(form3, { attr: {novalidate: 'novalidate'} }) }}
                        <section class="col-lg-9 col-md-9 col-sm-9 col-xs-9" style="position: relative; left: 5vmin;top: 6vmin">
                            <label style="display:inline-table;">
                                <span>{{form_widget(form3.idprof, {attr: { value : profil.id}} )}}</span>
                            </label>
                        </section>
                        <section class="col-lg-5 col-md-5 col-sm-5 col-xs-5" style="position: relative; left: 5vmin;top: 6vmin">
                            <span>{{ form_widget(form3.Confirmer)  }}</span>
                        </section>
                        {{ form_end(form3) }}
                    {% endif %}
                    </div>
                {% endfor %}
            </table>
           </div>
        {% else %}
                {{ "Aucun profil n'a été trouvé." }}
        {% endif %}


  /**
     * @Route("/webmaster/gestProf/{idprof}",  defaults={"idprof": 0},name="gestProf")
     * Method ("POST")
     * @Template()
     */
    public function gestProfAction(Request $request)
    {
        $session = new Session();
        $session->start();

        $em=$this
            ->getDoctrine()
            ->getManager();
        $repository = $em->getRepository("CNAMCMSBundle:profil");
        $profils = $repository->findAll();


        foreach ($profils as $prof) {
            $id = $prof->getId();
            $libelle = $prof->getLibelle();

        }
        $prof = new profil();
        $form3 = $this->createFormBuilder($prof, array('csrf_protection' => false))
            ->setMethod("POST")
            ->add('idprof', 'hidden', array('mapped' => false))
            ->add('Confirmer', 'submit', array('attr' => array('class' => 'btn btn-primary btn-block rounded_btn', 'id' => 'login_btn',
                'style' => "width:6vw;height:5vh;padding:0px 0px; position:relative;left:5vmin;top:1vmin;font-size:2vmin;")))
            ->getForm();
             $form3->handleRequest($request);
        if ($form3->isSubmitted()) {
            //$idprof = $request->request->get('idprof');
            $idprof = $form3->get('idprof')->getData();
            $repository = $em->getRepository("CNAMCMSBundle:profil");
            $prof = $repository->findOneById($idprof);

                $em=$this
                    ->getDoctrine()
                    ->getManager();
                $em->remove($prof);
                $em->flush();
            }

        var_dump($request->getContent());
        return $this->render('CNAMCMSBundle:Default:gestProf.html.twig', array('profils'=>$profils,
             'form3'=>$form3->createView()

        ));
    }

0 个答案:

没有答案