<?php
namespace App\Controller;
use App\Entity\Feed;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
/**
* @Route("", name="index")
*/
public function index(Request $request, EntityManagerInterface $em): Response
{
$fb = new Feed();
$form = $this->createCrearForm($fb);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($fb);
$em->flush();
$this->addFlash('success', '¡Feed enviado correctamente! Gracias por su comentario.');
return $this->render('home/index.html.twig', [
'form' => $form->createView(),
]);
}
return $this->render('home/index.html.twig', [
'form' => $form->createView(),
]);
}
/**
* Funcion auxiliar que crea el form para crear una pregunta
*/
private function createCrearForm(Feed $entity)
{
$builder = $this->createFormBuilder($entity);
$builder->setAction($this->generateUrl('index'));
$builder->setMethod('POST');
$builder->add('feedback', TextareaType::class, array('required' => true));
$builder->add('email', EmailType::class, array('required' => true));
$builder->add('nombre', TextType::class, array('required' => true));
$builder->add('_token', HiddenType::class, array('mapped' => false));
return $builder->getForm();
}
}