From f23be44b9a27fb035a743e93a005d1d68fb7417e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 23 Nov 2021 15:06:47 +0100 Subject: [PATCH] fix: show validation message when closing course --- CHANGELOG.md | 1 + .../AccompanyingCourseController.php | 26 ++++++++++++++----- .../translations/messages.fr.yml | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bd72198f..4acb29efc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to * Use the user.label in accompanying course banner, instead of username; +* fix: show validation message when closing accompanying course; ## Test releases diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseController.php index f1f49ff4d..fcef698ad 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseController.php @@ -22,9 +22,12 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\SerializerInterface; +use Symfony\Component\Validator\ConstraintViolationInterface; +use Symfony\Component\Validator\ConstraintViolationListInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Workflow\Registry; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use function is_array; /** @@ -42,18 +45,22 @@ class AccompanyingCourseController extends Controller private AccompanyingPeriodWorkRepository $workRepository; + private TranslatorInterface $translator; + public function __construct( SerializerInterface $serializer, EventDispatcherInterface $dispatcher, ValidatorInterface $validator, AccompanyingPeriodWorkRepository $workRepository, - Registry $registry + Registry $registry, + TranslatorInterface $translator ) { $this->serializer = $serializer; $this->dispatcher = $dispatcher; $this->validator = $validator; $this->workRepository = $workRepository; $this->registry = $registry; + $this->translator = $translator; } /** @@ -64,7 +71,9 @@ class AccompanyingCourseController extends Controller { $this->denyAccessUnlessGranted(AccompanyingPeriodVoter::EDIT, $accompanyingCourse); - $form = $this->createForm(AccompanyingCourseType::class, $accompanyingCourse); + $form = $this->createForm(AccompanyingCourseType::class, $accompanyingCourse, [ + 'validation_groups' => [AccompanyingPeriod::STEP_CLOSED] + ]); $form->handleRequest($request); @@ -74,11 +83,7 @@ class AccompanyingCourseController extends Controller $workflow = $this->registry->get($accompanyingCourse); if ($workflow->can($accompanyingCourse, 'close')) { - $errors = $this->validator->validate($accompanyingCourse, null, [$accompanyingCourse::STEP_CLOSED]); - if (count($errors) > 0) { - return $this->json($errors, 422); - } $workflow->apply($accompanyingCourse, 'close'); $em->flush(); @@ -86,6 +91,15 @@ class AccompanyingCourseController extends Controller return $this->redirectToRoute('chill_person_accompanying_course_index', [ 'accompanying_period_id' => $accompanyingCourse->getId(), ]); + } else { + /** @var ConstraintViolationListInterface $errors */ + $errors = $this->validator->validate($accompanyingCourse, null, [$accompanyingCourse::STEP_CLOSED]); + $this->addFlash('error', $this->translator->trans('It is not possible to close this course')); + + foreach ($errors as $e) { + /** @var ConstraintViolationInterface $e */ + $this->addFlash('error', $e->getMessage()); + } } } diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 66efc8335..d8860c8a1 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -169,6 +169,7 @@ Update accompanying period: Mettre à jour une période d'accompagnement 'Period not opened': "La période d'accompagnement n'a pas été ouverte" "Period not opened : form is invalid": "La période n'a pas été ouverte: le formulaire est invalide." 'Closing motive': 'Motif de clôture' +Close accompanying course: Clôturer le parcours 'Person details': 'Détails de la personne' 'Update details for %name%': 'Modifier détails de %name%' An accompanying period ends: Une periode d'accompagnement se clôture