php – 从另一个Action保存表单
我有两个动作,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. 我试着用 return $this->redirect($this->generateUrl('get_post',array('error',$form->getErrors()))); 和’error_bubbling’=>是的,但每次请求一个get_post(GetAllPost)我做一个新的表单渲染,我没有看到错误 例如,我想在几个场景中使用newCommentAction. 我有创建服务吗? UPDATE 经过Bonswouar的回答.这是我的代码 /** * @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,); } 我没有任何渲染. 解决方法
如果我没有误会,你的问题是你在new_comment上发帖,这是一个“子动作”.
实际上你不需要这个Twig渲染. 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); ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |