Several forms include symfony (on the same page)

I have a problem with multiple symfony (include).

I have several articles on one page, the user can add comments for each article. I would like to show a form to add a comment.

My problem is this: when I submit a comment form, Symfony does not save the information in the database (without an error message).

I tried to add a class to change the name of my form, but this is the same.

My form action in the controller (quiz call for each article)

public function formAction($post_id, Request $request) { $user = $this->getUser(); $em = $this->getDoctrine()->getManager(); $post = $em->getRepository('AppBundle:Post')->find($post_id); $comment = new Comment(); $comment -> setPost($post); $comment -> setAuthor($user); $form_comment = $this->createForm(CommentType::class, $comment); $form_comment->handleRequest($request); if ($form_comment->isSubmitted()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); $request->getSession()->getFlashBag()->add('msg_success', 'Votre contribution a bien รฉtรฉ ajoutรฉe'); } return $this->render('account/form-comment.html.twig', array( 'form_comment' => $form_comment->createView(), 'post_id' => $post_id )); } 

Get name in form type (for unique identifier)

 public function getName() { return self::NAME . '_' . uniqid(); } 
+5
source share
3 answers

Thanks for your reply!

Finally, there was no problem. I used the specific twig view for my comment form-comment.html.twig (include in index), but I did not specify a specific route path.

Two response elements:

First one . I use {{ render(url('comment-add')) }} instead of render(controller("AppBundle:Default:method") to enable the twig view.

Second one . I am specifying the path for commenting the action comment with my route name (comment-add). {{ form_start(form_comment, {'attr': {'method' : 'post', 'action' : path('comment-add', {'post_id': post_id, 'groupe_id' : groupe_id})}}) }}

(Sorry, because I did not indicate that I used the form with the inclusion of the twig view.)

0
source

If I am not mistaken, the problem arises from the getName() method with uniqid() .
The name in your controller does not match your branch, so your form is invalid.

I assume that you want to change the name of the form to have different identifiers in your view, if so, you can put the identifier in your form as follows:

 {{ form_start(form_comment, {'attr': {'id' : uniqvalue}}) }} 

with uniqvalue which can be a counter

+1
source

The most likely problem is to use uniqid() when generating the form type name. You see that at the first request, the name of the comment forms, for example, form_comment_567c0dff1b74a , and when the user form_comment_567c0dff1b74a on send comments and send data to the server, symfony will create the form again and link the requests to the data in the form, this is how symfony forms work, and in this In the case, the name of the form will be different from the name that I wrote above. Because of this, $form_comment->isSubmitted() will be false . Have you just tried without using uniqid() ? I am sure that it will work simply with a form type name, as usual, with a static name.

0
source

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


All Articles