Forms 如何在Symfony2中提交评论?

Forms 如何在Symfony2中提交评论?,forms,symfony,comments,submit,Forms,Symfony,Comments,Submit,我想在主页上提交评论,然后重定向到此页面。 这是我的行动: public function submitcommentAction(Request $request) { $user=$this->get('security.context')->getToken()->getUser(); $uid=$user->getId(); $comment=new comment(); //$comment->setCafe();

我想在主页上提交评论,然后重定向到此页面。 这是我的行动:

public function submitcommentAction(Request $request)
{
    $user=$this->get('security.context')->getToken()->getUser();
       $uid=$user->getId();

    $comment=new comment();

    //$comment->setCafe();

    $form=$this->createFormBuilder($comment)
            ->add('cafe','hidden')
            ->add('description','textarea')
            ->getForm();
    if($request->getMethod()=='POST')
    {
        $form->bind($request);

        if($form->isValid())
        {
            $comment->setCafe($form['cafe']->getData());
            $comment->setUser($uid);
            $comment->setDescription($form['description']->getData());
            $em=$this->getDoctrine()->getEntityManager();
            $em->persist($comment);
            $em->flush();
              // return $this->redirect($this->generateUrl('showcofe'));   
            var_dump($em);
    exit();

        }
    }

                    $r='error';
        return $this->      render('CafeManagementBundle:Default:simple.html.twig',array('r'=>$r));

}
这是我的小树枝:

    <div class="new-comment">
      <div class="new-comment-text"> :your comment
        <div>
          <form id="comment-form" method="post" action={{path('subcomment')}}" {{form_enctype(form1)}}">
            <ul class="smallbuttons">

                <input type="hidden" id="form_cafe" name="form[cafe]" value="{{cafe.id}}" />
                  {{form_widget(form1.description)}}
                <div style="display:none">{{ form_rest(form1) }}</div>

         <p><a id="clickbotton1"     href="javascript:document.getElementById('form1').submit()" class="btn_smallorange edit2">submit</a></p> 
            </ul>
          </form>
        </div>
      </div>
    </div>

:你的评论
    {{form_小部件(form1.description)} {{form_rest(form1)}


我不知道如何将隐藏字段(cafe)传递给控制器。另外,您能告诉我,我的控制器是否正确吗?

您正在使用Symfony2执行常规PHP。你的代码中有很多错误。首先,您的注释对象应该由表单本身使用请求数据进行水合(这就是它的工作!)。对于Cafe变量,也许可以从URI设置它?你应该看看文档,有很多例子:然后你似乎没有以正确的方式提交表单。您的表单HTML id是“commentform”,您正试图用javascript锚提交“form1”。document.getElementById('form1').submit()应替换为document.getElementById('comment-form').submit()。但为什么不添加一个提交按钮而不是这个链接呢?我不想在url中使用cafe id。我在我的行动中使用了隐藏选项,但它没有任何帮助:(我将在Flo Schield Boddy上测试它。谢谢