From 1d3ef1bfb8d75b8903b305d3554db1aee7fe8961 Mon Sep 17 00:00:00 2001 From: Hugo PRADIER Date: Wed, 5 Jun 2024 11:58:28 +0200 Subject: [PATCH] add form post --- src/Controller/PostController.php | 30 +++++++++++++++++++++++- src/Entity/Post.php | 4 ++++ src/Form/PostType.php | 39 +++++++++++++++++++++++++++++++ templates/post/post.html.twig | 3 +++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/Form/PostType.php create mode 100644 templates/post/post.html.twig diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 0df3016..93c5db1 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -4,8 +4,12 @@ namespace App\Controller; use App\Repository\PostRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use App\Entity\Post; +use App\Form\PostType; +use Doctrine\ORM\EntityManagerInterface; class PostController extends AbstractController { @@ -17,4 +21,28 @@ class PostController extends AbstractController 'posts' => $posts, ]); } + + #[Route('/posts', name: 'app_posts')] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $post = new Post(); + + $form = $this->createForm(PostType::class, $post); + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + + $post = $form->getData(); + $post->setPublicationDate(new \DateTimeImmutable("now")); + + $entityManager->persist($post); + $entityManager->flush(); + + return $this->redirectToRoute('app_posts'); + } + + return $this->render('post/post.html.twig', [ + 'form' => $form->createView(), + ]); + } } diff --git a/src/Entity/Post.php b/src/Entity/Post.php index 17ce70c..2bd8488 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -21,15 +21,19 @@ class Post private ?\DateTimeImmutable $publicationDate = null; #[ORM\Column(nullable: true)] + #[Assert\NotBlank] private ?float $latitude = null; #[ORM\Column(nullable: true)] + #[Assert\NotBlank] private ?float $longitude = null; #[ORM\Column(nullable: true)] + #[Assert\NotBlank] private ?float $altitude = null; #[ORM\Column(type: Types::TEXT)] + #[Assert\NotBlank] private ?string $commentary = null; #[ORM\ManyToOne(inversedBy: 'posts')] diff --git a/src/Form/PostType.php b/src/Form/PostType.php new file mode 100644 index 0000000..fef3b0c --- /dev/null +++ b/src/Form/PostType.php @@ -0,0 +1,39 @@ +add('foundDate', null, [ + 'widget' => 'single_text', + ]) + ->add('latitude') + ->add('longitude') + ->add('altitude') + ->add('commentary') + ->add('species', EntityType::class, [ + 'class' => Species::class, + 'choice_label' => 'scientific_name', + ]) + ->add('save', SubmitType::class, ['label' => 'Create Post']); + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Post::class, + ]); + } +} diff --git a/templates/post/post.html.twig b/templates/post/post.html.twig new file mode 100644 index 0000000..2727393 --- /dev/null +++ b/templates/post/post.html.twig @@ -0,0 +1,3 @@ +{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }} + {{ form_widget(form) }} +{{ form_end(form) }}