Create form to create offering

This commit is contained in:
Jannis Portmann 2021-04-30 17:08:34 +02:00
parent 635c8e900c
commit 990da0e669
5 changed files with 37 additions and 6 deletions

View file

@ -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(),