From fdae2ae025b6d42760e95cfa23109d078afcd10e Mon Sep 17 00:00:00 2001 From: Pablo Rodriguez Date: Sun, 28 Jul 2024 02:32:47 -0400 Subject: [PATCH] Implement comment form type --- src/Controller/ConferenceController.php | 23 ++++++++++++- src/Entity/Comment.php | 5 +++ src/Form/CommentType.php | 43 +++++++++++++++++++++++++ templates/conference/show.html.twig | 4 +++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/Form/CommentType.php diff --git a/src/Controller/ConferenceController.php b/src/Controller/ConferenceController.php index 171698b..8aee2b1 100644 --- a/src/Controller/ConferenceController.php +++ b/src/Controller/ConferenceController.php @@ -2,9 +2,12 @@ namespace App\Controller; +use App\Entity\Comment; use App\Entity\Conference; +use App\Form\CommentType; use App\Repository\CommentRepository; use App\Repository\ConferenceRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -13,6 +16,11 @@ use Twig\Environment; class ConferenceController extends AbstractController { + public function __construct( + private EntityManagerInterface $entityManager, + ){ + } + #[Route('/', name: 'homepage')] public function index(ConferenceRepository $conferenceRepository): Response { @@ -24,6 +32,18 @@ class ConferenceController extends AbstractController #[Route('/conference/{slug}', name: 'conference')] public function show(Request $request, Conference $conference, CommentRepository $commentRepository): Response { + $comment = new Comment(); + $form = $this->createForm(CommentType::class, $comment); + $form->handleRequest($request); + if($form->isSubmitted() && $form->isValid()) { + $comment->setConference($conference); + dump($comment); + $this->entityManager->persist($comment); + $this->entityManager->flush(); + + return $this->redirectToRoute('conference', ['slug' => $conference->getSlug()]); + } + $offset = max(0, $request->query->getInt('offset', 0)); $paginator = $commentRepository->getCommentPaginator($conference, $offset); @@ -31,7 +51,8 @@ class ConferenceController extends AbstractController 'conference' => $conference, 'comments' => $paginator, 'previous' => $offset - CommentRepository::COMMENTS_PER_PAGE, - 'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE) + 'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE), + 'comment_form' => $form, ]); } } diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index 58faa1f..677c629 100644 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -5,6 +5,7 @@ namespace App\Entity; use App\Repository\CommentRepository; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Symphony\Component\Validator\Constraints as Assert; #[ORM\Entity(repositoryClass: CommentRepository::class)] #[ORM\HasLifecycleCallbacks] @@ -16,12 +17,16 @@ class Comment private ?int $id = null; #[ORM\Column(length: 255)] + #[Assert\NotBlank] private ?string $author = null; #[ORM\Column(type: Types::TEXT)] + #[Assert\NotBlank] private ?string $text = null; #[ORM\Column(length: 255)] + #[Assert\NotBlank] + #[Assert\Email] private ?string $email = null; #[ORM\Column] diff --git a/src/Form/CommentType.php b/src/Form/CommentType.php new file mode 100644 index 0000000..8e3b83c --- /dev/null +++ b/src/Form/CommentType.php @@ -0,0 +1,43 @@ +add('author', null, [ + 'label' => 'Your name' + ]) + ->add('text') + ->add('email', EmailType::class) + ->add('photo', FileType::class, [ + 'required' => false, + 'mapped' => false, + 'constraints' => [ + new Image(['maxSize' => '1024k']) + ], + ]) + ->add('submit', SubmitType::class) + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Comment::class, + ]); + } +} diff --git a/templates/conference/show.html.twig b/templates/conference/show.html.twig index 8b258d9..f203649 100644 --- a/templates/conference/show.html.twig +++ b/templates/conference/show.html.twig @@ -30,4 +30,8 @@ {% else %}
No comments have been posted yet for this conference.
{% endif %} + +

Add your own feedback

+ + {{ form(comment_form) }} {% endblock %}