Created
February 1, 2019 16:30
-
-
Save ollo-ride-nico/4849795d01a17f6cff1705afd0a6e935 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 | |
) | |
{ | |
$task = new Task(); | |
// On récupère l 'utilisateur de la session | |
$user = $security->getUser(); | |
$form = $formFactory->create(TaskType::class, $task); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
// 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' => $form->createView()] | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment