Skip to content

Commit 14e6114

Browse files
committed
minor #461 Minor refactor to new comment action (yceruto)
This PR was merged into the master branch. Discussion ---------- Minor refactor to new comment action I couldn't find a text where this was explicitly mentioned, but the code above seems unconventional with respect to what the documentation shows and the rest of the application code. Refs: * http://symfony.com/doc/current/best_practices/forms.html#building-forms * http://symfony.com/doc/current/best_practices/forms.html#handling-form-submits * http://symfony.com/doc/current/forms.html#building-the-form * http://symfony.com/doc/current/forms.html#handling-form-submissions * https://github.com/symfony/symfony-demo/blob/master/src/AppBundle/Controller/Admin/BlogController.php#L75-L93 Commits ------- 5c11bcd Refactor new comment action
2 parents 14020ea + 5c11bcd commit 14e6114

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/AppBundle/Controller/BlogController.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,15 @@ public function postShowAction(Post $post)
9292
*/
9393
public function commentNewAction(Request $request, Post $post)
9494
{
95-
$form = $this->createForm(CommentType::class);
95+
$comment = new Comment();
96+
$comment->setAuthor($this->getUser());
97+
$comment->setPost($post);
98+
99+
$form = $this->createForm(CommentType::class, $comment);
96100

97101
$form->handleRequest($request);
98102

99103
if ($form->isSubmitted() && $form->isValid()) {
100-
/** @var Comment $comment */
101-
$comment = $form->getData();
102-
$comment->setAuthor($this->getUser());
103-
$comment->setPost($post);
104-
105104
$entityManager = $this->getDoctrine()->getManager();
106105
$entityManager->persist($comment);
107106
$entityManager->flush();

0 commit comments

Comments
 (0)