From 2b770036a566004c2e6c0f8a8ef6f73433563cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 20 May 2022 19:44:28 +0200 Subject: [PATCH] fix behaviour of CalendarType in Controller and add field users to CalendarType --- .../Controller/CalendarController.php | 5 ++--- .../ChillCalendarBundle/Form/CalendarType.php | 4 ++++ .../Resources/config/services/form.yml | 14 +++++--------- .../Tests/Form/CalendarTypeTest.php | 2 ++ .../DataTransformer/IdToEntityDataTransformer.php | 4 ++++ .../ChillMainBundle/config/services/form.yaml | 4 ++++ 6 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php index edc108f5d..932addd27 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php @@ -295,9 +295,8 @@ class CalendarController extends AbstractController $entity->setAccompanyingPeriod($accompanyingPeriod); } - $form = $this->createForm(CalendarType::class, $entity, [ - 'accompanyingPeriod' => $accompanyingPeriod, - ])->handleRequest($request); + $form = $this->createForm(CalendarType::class, $entity); + $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->persist($entity); diff --git a/src/Bundle/ChillCalendarBundle/Form/CalendarType.php b/src/Bundle/ChillCalendarBundle/Form/CalendarType.php index 2827a9035..37bb97c1d 100644 --- a/src/Bundle/ChillCalendarBundle/Form/CalendarType.php +++ b/src/Bundle/ChillCalendarBundle/Form/CalendarType.php @@ -129,6 +129,10 @@ class CalendarType extends AbstractType ->addModelTransformer($this->partiesToIdDataTransformer); */ + $builder->add('users', HiddenType::class); + $builder->get('users') + ->addModelTransformer($this->idToUsersDataTransformer); + $builder->add('calendarRange', HiddenType::class); $builder->get('calendarRange') ->addModelTransformer($this->calendarRangeDataTransformer); diff --git a/src/Bundle/ChillCalendarBundle/Resources/config/services/form.yml b/src/Bundle/ChillCalendarBundle/Resources/config/services/form.yml index 85095dc9a..310fce05f 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/config/services/form.yml +++ b/src/Bundle/ChillCalendarBundle/Resources/config/services/form.yml @@ -1,10 +1,6 @@ --- -services: - chill.calendar.form.type.calendar: - class: Chill\CalendarBundle\Form\CalendarType - arguments: - - "@chill.main.helper.translatable_string" - - "@doctrine.orm.entity_manager" - - tags: - - { name: form.type, alias: chill_calendarbundle_calendar } \ No newline at end of file +services: + Chill\CalendarBundle\Form\: + resource: './../../Form' + autowire: true + autoconfigure: true diff --git a/src/Bundle/ChillCalendarBundle/Tests/Form/CalendarTypeTest.php b/src/Bundle/ChillCalendarBundle/Tests/Form/CalendarTypeTest.php index 1f3671742..eb8b78d94 100644 --- a/src/Bundle/ChillCalendarBundle/Tests/Form/CalendarTypeTest.php +++ b/src/Bundle/ChillCalendarBundle/Tests/Form/CalendarTypeTest.php @@ -107,6 +107,8 @@ final class CalendarTypeTest extends TypeTestCase $this->assertEquals(8, $calendar->getCalendarRange()->getId()); $this->assertEquals(9, $calendar->getLocation()->getId()); $this->assertEquals(true, $calendar->getSendSMS()); + $this->assertContains(2, $calendar->getUsers()->map(function (User $u) { return $u->getId(); })); + $this->assertContains(3, $calendar->getUsers()->map(function (User $u) { return $u->getId(); })); } protected function getExtensions() diff --git a/src/Bundle/ChillMainBundle/Form/DataTransformer/IdToEntityDataTransformer.php b/src/Bundle/ChillMainBundle/Form/DataTransformer/IdToEntityDataTransformer.php index 4e565ef0e..9df6d053d 100644 --- a/src/Bundle/ChillMainBundle/Form/DataTransformer/IdToEntityDataTransformer.php +++ b/src/Bundle/ChillMainBundle/Form/DataTransformer/IdToEntityDataTransformer.php @@ -88,6 +88,10 @@ class IdToEntityDataTransformer implements DataTransformerInterface return implode(',', $ids); } + if (null === $value) { + return ''; + } + $id = call_user_func($this->getId, $value); if (null === $id) { diff --git a/src/Bundle/ChillMainBundle/config/services/form.yaml b/src/Bundle/ChillMainBundle/config/services/form.yaml index f2dc7803a..6570f8617 100644 --- a/src/Bundle/ChillMainBundle/config/services/form.yaml +++ b/src/Bundle/ChillMainBundle/config/services/form.yaml @@ -143,3 +143,7 @@ services: Chill\MainBundle\Form\Type\LocationFormType: ~ Chill\MainBundle\Form\WorkflowStepType: ~ + + Chill\MainBundle\Form\DataTransformer\IdToLocationDataTransformer: ~ + Chill\MainBundle\Form\DataTransformer\IdToUserDataTransformer: ~ + Chill\MainBundle\Form\DataTransformer\IdToUsersDataTransformer: ~