加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从另一个Action保存表单

发布时间:2020-12-13 16:55:06 所属栏目:PHP教程 来源:网络整理
导读:我有两个动作,GetAllPost和newComment 我有一个页面有很多帖子,每个帖子都有commentForm PostController中 public function getPostAction () { return array( );} 枝条 {% for post in app.user.posts %} p{{ post.id }} - {{ post.description }} /p {{ re
我有两个动作,GetAllPost和newComment
我有一个页面有很多帖子,每个帖子都有commentForm

PostController中

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%}

CommentController

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(),);
    }

TwifFormController

{{ 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’=>是的,但每次请求一个get_post(GetAllPost)我做一个新的表单渲染,我没有看到错误

例如,我想在几个场景中使用newCommentAction.
例如,每个帖子都有我的GetAllPost,但即使在GetSpecificPost,我有一个特定的帖子,我可以插入一个新的评论,但保存(和动作)是相同的.

我有创建服务吗?

UPDATE

经过Bonswouar的回答.这是我的代码
PostController中

/**
     * @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,);
    }

我没有任何渲染.
但是,我想在单一帖子中重新使用newCommentAction,我想创建只有一个表单.我不想使用$commentForms = $this-> createCreateForm($comment);,因为我只想要一个表单,我甚至需要更改模板.我能怎么做 ?

解决方法

如果我没有误会,你的问题是你在new_comment上发帖,这是一个“子动作”.

实际上你不需要这个Twig渲染.
您可以在主Action中生成所需的所有表单,如下所示:

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,);

不要忘记在Form类中设置动态名称:

class CommentType extends AbstractType
{
     public function __construct($id) {
         $this->postId = $id;
     }
     public function getName() {
         return 'your_form_name'.$this->postId;
     }
     //...
}

然后通常只需在Twig循环中渲染表单.你应该得到错误.

{% 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);
    );
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读