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

@ -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";

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

View file

@ -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;
/**

View file

@ -4,20 +4,28 @@ 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)
;
}

View file

@ -1,5 +1,6 @@
{% extends 'base.html.twig' %}
{% block body %}
<h1 class="mb-3">Add new offering</h1>
{{ form(offering_form) }}
{% endblock %}