From 990da0e66993f7f09273408d48cf1c639ee6fcc8 Mon Sep 17 00:00:00 2001 From: thisfro Date: Fri, 30 Apr 2021 17:08:34 +0200 Subject: [PATCH] Create form to create offering --- assets/styles/app.scss | 2 +- src/Controller/AppController.php | 21 ++++++++++++++++++++- src/Entity/Offering.php | 3 +++ src/Form/OfferingFormType.php | 16 ++++++++++++---- templates/app/new_listing.html.twig | 1 + 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/assets/styles/app.scss b/assets/styles/app.scss index dcf5dcd..ccb0051 100644 --- a/assets/styles/app.scss +++ b/assets/styles/app.scss @@ -1,7 +1,7 @@ // assets/styles/global.scss // customize some Bootstrap variables -$primary: darken(#428bca, 20%); +$primary: darken(#005035, 20%); // the ~ allows you to reference things in node_modules @import "~bootstrap/scss/bootstrap"; diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index d7c0f74..55670d4 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -6,13 +6,22 @@ use App\Entity\Offering; use App\Form\OfferingFormType; use App\Repository\OfferingRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Twig\Environment; class AppController extends AbstractController { + private $entityManager; + + public function __construct(EntityManagerInterface $entityManager) + { + $this->entityManager = $entityManager; + } + #[Route('/', name: 'homepage')] public function index(Environment $twig, OfferingRepository $offeringRepository): Response { @@ -30,11 +39,21 @@ class AppController extends AbstractController } #[Route('/new', name: 'new_listing')] - public function new_listing(): Response + public function new_listing(Request $request): Response { $offering = new Offering(); $form = $this->createForm(OfferingFormType::class, $offering); + $user = $this->getUser(); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $offering->setByUser($user); + $offering->setCreatedAt(new \DateTime()); + + $this->entityManager->persist($offering); + $this->entityManager->flush(); + } return $this->render('app/new_listing.html.twig', [ 'user' => $this->getUser(), diff --git a/src/Entity/Offering.php b/src/Entity/Offering.php index 67e9555..30f3cc8 100644 --- a/src/Entity/Offering.php +++ b/src/Entity/Offering.php @@ -4,6 +4,7 @@ namespace App\Entity; use App\Repository\OfferingRepository; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity(repositoryClass=OfferingRepository::class) @@ -31,6 +32,7 @@ class Offering /** * @ORM\Column(type="string", length=255) */ + #[Assert\NotBlank] private $title; /** @@ -41,6 +43,7 @@ class Offering /** * @ORM\Column(type="integer") */ + #[Assert\NotBlank] private $zipCode; /** diff --git a/src/Form/OfferingFormType.php b/src/Form/OfferingFormType.php index 41fa2b4..c734894 100644 --- a/src/Form/OfferingFormType.php +++ b/src/Form/OfferingFormType.php @@ -4,21 +4,29 @@ namespace App\Form; use App\Entity\Offering; use Symfony\Component\Form\AbstractType; +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 OfferingFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('createdAt') ->add('title') - ->add('photoFilename') ->add('zipCode') ->add('description') - ->add('byUser') - ; + ->add('photo', FileType::class, [ + 'required' => false, + 'mapped' => false, + 'constraints' => [ + new Image(['maxSize' => '1024k']) + ], + ]) + ->add('submit', SubmitType::class) + ; } public function configureOptions(OptionsResolver $resolver) diff --git a/templates/app/new_listing.html.twig b/templates/app/new_listing.html.twig index 6878753..8d3e70e 100644 --- a/templates/app/new_listing.html.twig +++ b/templates/app/new_listing.html.twig @@ -1,5 +1,6 @@ {% extends 'base.html.twig' %} {% block body %} +

Add new offering

{{ form(offering_form) }} {% endblock %} \ No newline at end of file