Compare commits

...

2 Commits

Author SHA1 Message Date
fdae2ae025 Implement comment form type 2024-07-28 02:32:47 -04:00
d090e3f36c Fixed typo in Twig template 2024-07-28 02:26:59 -04:00
4 changed files with 75 additions and 2 deletions

View File

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

View File

@@ -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]

43
src/Form/CommentType.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace App\Form;
use App\Entity\Comment;
use App\Entity\Conference;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Image;
class CommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->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,
]);
}
}

View File

@@ -3,7 +3,7 @@
{% block title %}Conference Guestbook - {{ conference }}{% endblock %}
{% block body %}
<h2>{{ conference }} Conference<h2>
<h2>{{ conference }} Conference</h2>
{% if comments|length > 0 %}
<div>There are {{ comments|length }} comments.</div>
@@ -30,4 +30,8 @@
{% else %}
<div>No comments have been posted yet for this conference.</div>
{% endif %}
<h2>Add your own feedback</h2>
{{ form(comment_form) }}
{% endblock %}