Implement comment form type
This commit is contained in:
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
43
src/Form/CommentType.php
Normal 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user