我有两个动作,GetAllPost和NewMoment
我有一个有很多帖子的页面,每个帖子都有评论表
后控制器

public function getPostAction () {
     return array(
    );
}

细枝
{% for post in app.user.posts %}
        <p>{{ post.id }} - {{ post.description }} </p>
        {{ render(controller("ADVCommentBundle:Comment:newComment" ,{ 'id': post.id,'redirect':'get_post' } )) }}
        <hr>
    {%endfor%}

注释控制器
public function newCommentAction (Request $request, Post $post) {
        $em = $this->getEm();
        $comment = new Comment();
        $form = $this->createForm(new CommentType(), $comment);
        $form->handleRequest($request);
            if ($form->isValid()) {
                try {
                    $em->beginTransaction();
                    $comment->setPost($post);
                    $em->persist($comment);
                    $em->flush();
                    $em->commit();
                } catch (\Exception $e) {
                    $em->rollback();
                    throw $e;
                }
            }
        return array(
            'post' => $post,
            'form' => $form->createView(),
        );
    }

旋转控制器
{{ form(form, {'action': path('new_comment',{'id': post.id})})}}

当我插入新注释时,即使我的值无效,我也会重定向到new_comment
如何重定向到GetAllPost并显示正确的错误或新注释?
我试着用
return $this->redirect($this->generateUrl('get_post',array('error',$form->getErrors())));

'error_bubbling' => true,,但每次请求get-post(getallpost)时,我都会对表单进行一次新的呈现,但看不到错误
例如,我想在几个场景中使用newcommentaction。
例如,我为每一篇文章都设置了getAllPost,但即使在getSpecificPost中,我也有一篇特定的文章,我可以在其中插入新的注释,但是保存(和操作)是相同的。
我有创建服务吗?
更新
在邦斯沃的回答之后。这是我的密码
后控制器
/**
     * @Route("/",name="get_posts")
     * @Template()
     */
    public function getPostsAction () {
        $comment = new Comment();
        return array(
            'commentForms' => $this->createCreateForm($comment),
        );
    }

    private function createCreateForm (Comment $entity) {
        $em = $this->getEm();
        $posts = $em->getRepository('ADVPostBundle:Post')->findAll();
        $commentForms = array();
        foreach ($posts as $post) {
            $form = $this->createForm(new CommentType($post->getId()), $entity);
            $commentForms[$post->getId()] = $form->createView();
        }
        return $commentForms;
    }


    /**
     * @Method({"POST"})
     * @Route("/new_comment/{id}",name="new_comment")
     * @Template("@ADVPost/Post/getPosts.html.twig")
     * @ParamConverter("post", class="ADVPostBundle:Post")
     */
    public function newCommentAction (Request $request, Post $post) {
        $em = $this->getEm();
        $comment = new Comment();

        //Sometimes I Have only One Form
        $commentForms = $this->createCreateForm($comment);

        $form = $this->createForm(new CommentType($post->getId()), $comment);
        $form->handleRequest($request);
        if ($form->isValid()) {
            try {
                $em->beginTransaction();
                $comment->setPost($post);
                $em->persist($comment);
                $em->flush();
                $em->commit();
            } catch (\Exception $e) {
                $em->rollback();
                throw $e;
            }
        } else {
            $commentForms[$post->getId()] = $form->createView();
        }

        return array(
            'commentForms' => $commentForms,
        );
    }

而且我没有任何渲染。
但是,我想在单个post中重用newCommentAction,并且只创建一个表单。我不想使用$commentForms = $this->createCreateForm($comment);,因为我只想要一个表单,而且我还要更改模板。我该怎么办?

最佳答案

如果我没有弄错的话,你的问题是你在new_comment上发帖,这是一个“子动作”。
你其实不需要这个树枝。
您可以在主操作中生成所需的所有表单,如下所示:

foreach ($posts as $post) {
  $form = $this->createForm(new CommentType($post->getId()), new Comment());
  $form->handleRequest($request);
  if ($form->isValid()) {
    //...
    // Edited : to "empty" the form if submitted & valid. Another option would be to redirect()
    $form = $this->createForm(new CommentType($post->getId()), new Comment());
  }
  $commentForms[$post->getId()] = $form->createView();
}
return array(
  'posts' => $posts,
  'commentForms' => $commentForms,
);

不要忘记在表单类中设置动态名称:
class CommentType extends AbstractType
{
     public function __construct($id) {
         $this->postId = $id;
     }
     public function getName() {
         return 'your_form_name'.$this->postId;
     }
     //...
}

然后在你的树枝循环中渲染你的表单。你应该得到错误。
{% for post in app.user.posts %}
    <p>{{ post.id }} - {{ post.description }} </p>
    {{ form(commentForms[post.id]) }}
    <hr>
{%endfor%}

如果我没有遗漏任何应该做这项工作的东西。
更新:
在看到您的更新后,这可能是您想要的控制器(很抱歉,如果我没有正确理解或我犯了一些错误):
/**
 * @Route("/",name="get_posts")
 * @Template()
 */
public function getPostsAction () {
    $em = $this->getEm();
    $posts = $em->getRepository('ADVPostBundle:Post')->findAll();
    $commentForms = array();
    foreach ($posts as $post) {
        $commentForms[$post->getId()] = $this->createCommentForm($post);
    }
    return array(
        'commentForms' => $commentForms
    );
}

private function createCommentForm (Post $post, $request = null) {
    $em = $this->getEm();
    $form = $this->createForm(new CommentType($post->getId()), new Comment());
    if ($request) {
        $form->handleRequest($request);
        if ($form->isValid()) {
            try {
                $em->beginTransaction();
                $comment->setPost($post);
                $em->persist($comment);
                $em->flush();
                $em->commit();
            } catch (\Exception $e) {
                $em->rollback();
                throw $e;
            }
            $form = $this->createForm(new CommentType($post->getId()), new Comment());
        }
    }
    return $form;
}


/**
 * @Method({"POST"})
 * @Route("/new_comment/{id}",name="new_comment")
 * @Template("@ADVPost/Post/getPosts.html.twig")
 * @ParamConverter("post", class="ADVPostBundle:Post")
 */
public function newCommentAction (Request $request, Post $post) {
    return array(
        'commentForm' => $this->createCommentForm($post, $request);
    );
}

10-02 16:33