diff --git a/src/Bundle/ChillMainBundle/Controller/WorkflowController.php b/src/Bundle/ChillMainBundle/Controller/WorkflowController.php index 33f1bd774..247cdf552 100644 --- a/src/Bundle/ChillMainBundle/Controller/WorkflowController.php +++ b/src/Bundle/ChillMainBundle/Controller/WorkflowController.php @@ -19,6 +19,7 @@ use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository; use Chill\MainBundle\Security\Authorization\EntityWorkflowVoter; use Chill\MainBundle\Workflow\EntityWorkflowManager; +use Chill\MainBundle\Workflow\Validator\StepDestValid; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -201,9 +202,18 @@ class WorkflowController extends AbstractController $entityWorkflow->getCurrentStep()->addDestUser($user); } - $this->entityManager->flush(); + $errors = $this->validator->validate( + $entityWorkflow->getCurrentStep(), + new StepDestValid() + ); - return $this->redirectToRoute('chill_main_workflow_show', ['id' => $entityWorkflow->getId()]); + if (count($errors) === 0) { + $this->entityManager->flush(); + + return $this->redirectToRoute('chill_main_workflow_show', ['id' => $entityWorkflow->getId()]); + } + + return new Response((string) $errors, Response::HTTP_UNPROCESSABLE_ENTITY); } if ($transitionForm->isSubmitted() && !$transitionForm->isValid()) { @@ -235,6 +245,7 @@ class WorkflowController extends AbstractController 'handler_template_data' => $handler->getTemplateData($entityWorkflow), 'transition_form' => isset($transitionForm) ? $transitionForm->createView() : null, 'entity_workflow' => $entityWorkflow, + 'transition_form_errors' => $errors ?? [], //'comment_form' => $commentForm->createView(), ] ); diff --git a/src/Bundle/ChillMainBundle/Resources/public/page/workflow-show/index.js b/src/Bundle/ChillMainBundle/Resources/public/page/workflow-show/index.js index f589ee7d5..bd05a8aaa 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/page/workflow-show/index.js +++ b/src/Bundle/ChillMainBundle/Resources/public/page/workflow-show/index.js @@ -38,8 +38,6 @@ window.addEventListener('DOMContentLoaded', function() { transitions.querySelectorAll('.form-check').forEach(function(row) { const isForward = row.querySelector('input').dataset.isForward; - console.log(row); - console.log(isForward); new ShowHide({ load_event: null, @@ -69,7 +67,24 @@ window.addEventListener('DOMContentLoaded', function() { }); } + // validate form + let form = document.querySelector('form[name="workflow_step"]'); + if (form === null) { + console.error('form to validate not found'); + } + form.addEventListener('submit', function (event) { + const datas = new FormData(event.target); + + if (datas.has('workflow_step[future_dest_users]')) { + const dests = JSON.parse(datas.get('workflow_step[future_dest_users]')); + if (dests === null || (dests instanceof Array && dests.length === 0)) { + event.preventDefault(); + console.log('no users!'); + window.alert('Indiquez un utilisateur pour traiter la prochaine étape.'); + } + } + }); }); diff --git a/src/Bundle/ChillMainBundle/Resources/views/Workflow/_decision.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Workflow/_decision.html.twig index e557d9e89..64482c067 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Workflow/_decision.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Workflow/_decision.html.twig @@ -46,8 +46,6 @@ - -
{% if transition_form.transitionFilter is defined %} {{ form_row(transition_form.transitionFilter) }} diff --git a/src/Bundle/ChillMainBundle/Workflow/Validator/StepDestValid.php b/src/Bundle/ChillMainBundle/Workflow/Validator/StepDestValid.php new file mode 100644 index 000000000..7c2d954b2 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Workflow/Validator/StepDestValid.php @@ -0,0 +1,26 @@ +isFinal() && 0 < count($value->getDestUser())) { + $this->context->buildViolation($constraint->messageDestNotAllowed) + ->addViolation(); + } + + if (!$value->isFinal() && 0 === count($value->getDestUser())) { + $this->context->buildViolation($constraint->messageRequireDest) + ->addViolation(); + } + } +}