Save form from another action

I have two actions, GetAllPost and newComment. I have a page with many posts, and each post has a commentForm

Postcontroller

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

Tuig

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

When I insert a new comment, I am redirected to new_comment, even if my value is invalid. How can I redirect to GeTAllPost and show the correct error or new comment?

I tried to use

return $this->redirect($this->generateUrl('get_post',array('error',$form->getErrors())));

and 'error_bubbling' => true,, but every time I request get_post (GetAllPost), I do a new render of my form, and I see no errors

newCommentAction . , GetAllPost , GetSpecificPost, , , ( ) .

?

UPDATE

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

Render. newCommentAction Single Post, . $commentForms = $this->createCreateForm($comment);, , . ?

+4
2

, , new_comment, "-".

Twig render. , 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);
    );
}
+3

- ? http://symfony.com/doc/current/components/http_foundation/sessions.html#flash-messages

EDIT: . :

foreach ($form->getErrors() as $error) {
    $this->addFlash('error', $post->getId().'|'.$error->getMessage());
}

$this->addFlash('error',  $post->getId().'|'.(string) $form->getErrors(true, false));

, , 355 | . , $error- > getPropertyPath() - .

- , :

$errors = array();

foreach ($this->get('session')->getFlashBag()->get('error', array()) as $error)
{
    list($postId, $message) = explode('|', $error);

    $errors[$postId][] = $message;
}

return array('errors' => $errors, ...anything else you send to the template)

:

{% for post in app.user.posts %}
    {% if errors[post.id] is defined %}
        <ul class="errors">
        {% for error_message in errors[post.id] %}
            <li>{{ error_message }}</li>
        {% endfor %}
        </ul>
    {% endif %}
    <p>{{ post.id }} - {{ post.description }} </p>
    {{ render(controller("ADVCommentBundle:Comment:newComment" ,{ 'id': post.id,'redirect':'get_post' } )) }}
    <hr>
{%endfor%}
0

Source: https://habr.com/ru/post/1568009/


All Articles