Created
February 4, 2019 15:48
-
-
Save ollo-ride-nico/b2271d8ab9292391e804d856ee9e28ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Route("/tasks/create", name="task_create") | |
* @param Request $request | |
* @param Security $security | |
* @param FormFactory $formFactory | |
* @param Session $session | |
* @param RouterInterface $router | |
* @return RedirectResponse|Response | |
*/ | |
public function createTask( | |
Request $request, | |
Security $security, | |
FormFactory $formFactory, | |
Session $session, | |
RouterInterface $router | |
): Response { | |
$task = new Task(); | |
$formHandler = new TaskHandler($formFactory->create(TaskType::class, $task), $request); | |
// On récupère l 'utilisateur de la session | |
$user = $security->getUser(); | |
//$form = $formFactory->create(TaskType::class, $task); | |
if ($formHandler->process()){ | |
// On attribut le 'username' de la session à la tache créée | |
$task->setUser($user); | |
$this->entityManager->persist($task); | |
$this->entityManager->flush(); | |
$session->getFlashBag()->add('success', 'La tâche a été bien été ajoutée.'); | |
return new RedirectResponse($router->generate('task_list')); | |
} | |
return new Response($this->twig->render( | |
'task/create.html.twig', | |
['form' => $formHandler->getForm()->createView()] | |
)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by Nicolas Dirollo. | |
* Date: 04/02/2019 | |
* Time: 16:22 | |
* @package Symfony | |
* @author Nicolas Dirollo | |
* @copyright 2019 ND | |
* @license * | |
*/ | |
namespace AppBundle\Form\Handler; | |
use Symfony\Component\Form\Form; | |
use Symfony\Component\HttpFoundation\Request; | |
class TaskHandler | |
{ | |
protected $form; | |
protected $request; | |
/** | |
* TaskHandler constructor. | |
* @param Form $form | |
* @param Request $request | |
*/ | |
public function __construct(Form $form, Request $request) | |
{ | |
$this->form = $form; | |
$this->request = $request; | |
} | |
/** | |
* @return bool | |
*/ | |
public function process() | |
{ | |
$this->form->handleRequest($this->request); | |
if ($this->form->isSubmitted() && $this->form->isValid()) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* @return Form | |
*/ | |
public function getForm() | |
{ | |
return $this->form; | |
} | |
/** | |
* | |
*/ | |
protected function onSuccess() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment