From c423a0f8ca636a811d05ed29d0be2057abefb8ae Mon Sep 17 00:00:00 2001 From: Kai Eichinger Date: Sat, 14 Jul 2018 21:34:42 +0200 Subject: [PATCH 1/3] Add necessary call to Form::isSubmitted and remove boilerplate - Since Symfony 4.0 it's required to call `Form::isSubmitted()` before you call `Form::isValid()` - Remove some unnecessary boilerplate code by using automatic parameter conversion and parameter injection of the `EntityManager` --- form/form_collections.rst | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/form/form_collections.rst b/form/form_collections.rst index 3291297c04c..03061a09bfe 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -655,15 +655,8 @@ the relationship between the removed ``Tag`` and ``Task`` object. use Doctrine\Common\Collections\ArrayCollection; // ... - public function edit($id, Request $request) + public function edit(Task $task, Request $request, EntityManagerInterface $entityManager) { - $entityManager = $this->getDoctrine()->getManager(); - $task = $entityManager->getRepository(Task::class)->find($id); - - if (!$task) { - throw $this->createNotFoundException('No task found for id '.$id); - } - $originalTags = new ArrayCollection(); // Create an ArrayCollection of the current Tag objects in the database @@ -675,7 +668,7 @@ the relationship between the removed ``Tag`` and ``Task`` object. $editForm->handleRequest($request); - if ($editForm->isValid()) { + if ($editForm->isSubmitted() && $editForm->isValid()) { // remove the relationship between the tag and the Task foreach ($originalTags as $tag) { From 08c177a79226ac703f318922a89636495b3408dc Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 23 Jul 2018 10:14:50 +0200 Subject: [PATCH 2/3] Minor refactor --- form/form_collections.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/form/form_collections.rst b/form/form_collections.rst index 03061a09bfe..b799a9c64b3 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -655,8 +655,12 @@ the relationship between the removed ``Tag`` and ``Task`` object. use Doctrine\Common\Collections\ArrayCollection; // ... - public function edit(Task $task, Request $request, EntityManagerInterface $entityManager) + public function edit($id, Request $request, EntityManagerInterface $entityManager) { + if (null === $task = $entityManager->getRepository(Task::class)->find($id)) { + throw $this->createNotFoundException('No task found for id '.$id); + } + $originalTags = new ArrayCollection(); // Create an ArrayCollection of the current Tag objects in the database @@ -669,7 +673,6 @@ the relationship between the removed ``Tag`` and ``Task`` object. $editForm->handleRequest($request); if ($editForm->isSubmitted() && $editForm->isValid()) { - // remove the relationship between the tag and the Task foreach ($originalTags as $tag) { if (false === $task->getTags()->contains($tag)) { From 1ed7a3323e5df31b673f5d200e8b6d4d03b813a6 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 23 Jul 2018 10:17:24 +0200 Subject: [PATCH 3/3] Update form_collections.rst --- form/form_collections.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/form/form_collections.rst b/form/form_collections.rst index b799a9c64b3..6c49243d68d 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -660,7 +660,7 @@ the relationship between the removed ``Tag`` and ``Task`` object. if (null === $task = $entityManager->getRepository(Task::class)->find($id)) { throw $this->createNotFoundException('No task found for id '.$id); } - + $originalTags = new ArrayCollection(); // Create an ArrayCollection of the current Tag objects in the database