Symfony:如何在同一个动作中处理2个表单?

时间:2017-03-22 21:42:00

标签: php symfony

我想在同一个动作中处理两种形式,实际上第一种形式效果很好,但另一种形式不起作用。这是我的行动代码:

 /**
     * @Route("/webmaster/gestProf/{idprof}",  defaults={"idprof": 0},name="gestProf")
     * @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();

        }
        $profil = new profil();
        $form = $this->createFormBuilder($profil, array('csrf_protection' => false))
            ->add('id', 'text', array('attr' => array('maxlength' => '255', 'placeholder' => 'Nouvel Identificateur', 'id' => 'id_prof')))
            ->add('libelle', 'text', array('attr' => array('maxlength' => '20', 'placeholder' => 'Nouveau Libellé', 'id' => 'libelle')))
            ->add('idprof', 'hidden', array('mapped' => false,'attr' => array('maxlength' => '20', 'placeholder' => 'Nouveau Libellé', 'id' => 'libelle')))
            ->add('Edit', '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();
        $profile = new profil();
        $form2 = $this->createFormBuilder($profile, array('csrf_protection' => false))
            ->add('id', 'text', array('attr' => array('maxlength' => '255', 'placeholder' => 'Nouvel Identificateur', 'id' => 'id_prof')))
            ->add('libelle', 'text', array('attr' => array('maxlength' => '20', 'placeholder' => 'Nouveau Libellé', 'id' => 'libelle')))
            ->add('Ajouter', '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();
        if ($request->request->has('form')) {
           $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $idprof = $form->get('idprof')->getData();
            $id = $data->getId();
            $libelle = $data->getLibelle();
            $em = $this
                ->getDoctrine()
                ->getManager();
            $repository = $em->getRepository("CNAMCMSBundle:profil");
            $profil = $repository->find($idprof);
            if ($id !== Null) {
                $profil->setId($id);
                $em->merge($profil);
                $em->flush();
            }

            if ($libelle !== Null) {
                $profil->setLibelle($libelle);
                $em->merge($profil);
                $em->flush();
            }
        }
    }
        if ($request->request->has('form2')) {
            $form2->handleRequest($request);
           // $id2 = $request->request->get('id');
           // $libelle2 = $request->request->get('libelle');
            if ($form2->isSubmitted() && $form2->isValid()) {
                $data2 = $form2->getData();
                //$idprof = $form->get('idprof')->getData();
                $id2 = $data2->getId();
                $libelle2 = $data2->getLibelle();
                $em=$this
                    ->getDoctrine()
                    ->getManager();
               $profile->setId($id2);
               $profile->setLibelle($libelle2);
                $em->persist($profile);
                $em->flush();
            }
        }

        return $this->render('CNAMCMSBundle:Default:gestProf.html.twig', array('profils'=>$profils,
            'form'=>$form->createView(),
             'form2'=>$form2->createView()

        ));
    }

这是他们在Twig中的实现:

{{ form_start(form, { 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(form.id) }}</span>
                                </label>
                            </section>
                            <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(form.libelle) }}</span>
                                </label>
                            </section>
                            <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(form.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(form.Edit)  }}</span>
                            </section>
                            {{ form_end(form) }}


 {{ form_start(form2, { 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(form2.id) }}</span>
                    </label>
                </section>
                <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(form2.libelle) }}</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(form2.Ajouter)  }}</span>
                </section>
                {{ form_end(form2) }}

2 个答案:

答案 0 :(得分:1)

首先,我建议从控制器中删除表单类,并将它们放在捆绑包的Form文件夹下的单独类中。 然后在这个类中为它们提供不同的前缀。

public function getBlockPrefix()
    {
        return 'form1';
    }

然后,正确命名所有表单字段,您不必手动将数据应用于每个实体字段。 同时为两个表单删除此行。

$request-request->has('form')

为什么在提交表单时使用$ em-&gt;合并而不是持久化。或者也许我错过了什么? 请尝试阅读此文档。 http://symfony.com/doc/current/forms.html Symfony有很棒的文档。

答案 1 :(得分:0)

我通过将方法帖子添加到我的第二个表单来解决我的问题:

  $form2 = $this->createFormBuilder($profile,array('csrf_protection' => false))
                ->setMethod("POST");