setAccompanyingPeriod($accompanyingCourse); $form = $this->createCommentForm($newComment, 'new'); $editForm = null; if ($request->query->has('edit')) { foreach ($accompanyingCourse->getComments() as $comment) { if ($comment->getId() === $request->query->getInt('edit')) { $editForm = $this->createCommentForm($comment, 'edit'); $commentEditId = $comment->getId(); } } $pinnedComment = $accompanyingCourse->getPinnedComment(); if ($pinnedComment->getId() === $request->query->getInt('edit')) { $editForm = $this->createCommentForm($pinnedComment, 'edit'); $commentEditId = $pinnedComment->getId(); } } dump($editForm); //if (null === $editForm) { // throw new NotFoundHttpException('Unable to find an edit form.'); //} if ($request->getMethod() === Request::METHOD_POST) { $currentForm = $editForm->handleRequest($request); if (array_key_exists('edit', $request->request->all()[$editForm->getName()])) { $isEditingNew = false; } else { $isEditingNew = true; } if ($currentForm->isSubmitted() && $currentForm->isValid()) { $em = $this->getDoctrine()->getManager(); if ($isEditingNew) { $em->persist($newComment); } $em->flush(); } return $this->redirectToRoute('chill_person_accompanying_period_comment_list', [ 'accompanying_period_id' => $accompanyingCourse->getId(), ]); } return $this->render('@ChillPerson/AccompanyingCourse/comment_list.html.twig', [ 'accompanyingCourse' => $accompanyingCourse, 'form' => $form->createView(), 'edit_form' => null !== $editForm ? $editForm->createView() : null, 'commentEditId' => $commentEditId ?? null, ]); } private function createCommentForm(AccompanyingPeriod\Comment $comment, string $step): FormInterface { $form = $this->createForm(AccompanyingCourseCommentType::class, $comment); if ('edit' === $step) { $form->add('edit', HiddenType::class, ['mapped' => false]); } return $form; } }