我无法从我的某个表单

时间:2016-05-11 01:27:59

标签: symfony symfony-2.6

我创建了一个包含两个表单的页面我希望在提交按钮时获取每​​个表单的数据,这里的问题是我可以只为表单中的一个获取数据

这是我的indexAction:

    public function indexCcpAction ()
{

    $demanceCCP = new DemandeCCP();
    $ccp = new Ccp();
    $formDemanceCCP = $this->createForm(new DemandeCcpType(), $demanceCCP);
    $formCcp = $this->createForm(new LoginCcpType(), $ccp);
    $formCcp->handleRequest($this->get('request'));
    $em = $this->getDoctrine()->getManager();
    if ( $this->get("request")->getMethod() == "POST" ) {
            if ($this->get("request")->request->has("DemandeCcpType")  ) {
                $demanceCCP = $formDemanceCCP->getData(); // i can't get data from this form
                echo($demanceCCP->getNom());
                $em->persist($demanceCCP);
                $em->flush();
            }
            if ($this->get("request")->request->has("LoginCcpType")) {
                $ccp = $formCcp->getData(); // but in this form work
                echo ($ccp->getMdp());
            }
    }
    return $this->render('EgovPosteBundle:Ccp:DemanceCCP.html.twig',
        array('formDemandeCcp'=>$formDemanceCCP->createView(),
              'formLogin'=>$formCcp->createView()));
}

当我尝试插入DemandeCcpType

的数据时,我收到此异常
  

执行' INSERT INTO DemandeCCP时发生异常(nom,   prenom,dateNaissance,lieuNaissance,profession,nationalite,   typePieceIdentite,numeroIdentite,adresse,email,tel,codePostal,   sollicite,dateDemancde,statut)VALUES(?,?,?,?,?,?,?,?,?,?,   ?,?,?,?,?)' with params [null,null,null,null,null,null,null,   null,null,null,null,null,null," 2016-05-11 03:25:36"," en cour"]:

     

SQLSTATE [23000]:违反完整性约束:1048列' nom'   不能为空

1 个答案:

答案 0 :(得分:2)

您只为其中一个表单调用了handleRequest。需要同时调用它们。

添加$formDemanceCCP->handleRequest($this->get('request'));