LexikWorkflowBundle icon indicating copy to clipboard operation
LexikWorkflowBundle copied to clipboard

Check whether it is possible to reach a step (canReachNextStep)

Open archie18 opened this issue 10 years ago • 3 comments

Hi, when trying to use LexikWorkflowBundle for our project the need arose to be able to check whether a process can reach a next step without actually advancing the model to the next step. In more practical terms, I would like to know whether a user of my system is able to submit a certain web form before actually showing the form to the user. So I probably need something along the lines of:

if ($processHandler->canReachNextState($model, 'validate')) {...}

Like this we avoid duplicating code in the workflow validators and form controllers.

Is there a function like this? I didn't find one.

If there is not, I would probably simply copy $processHandler::reachNextState() and adapt the method to my needs. This rises the next question:

What is the best way to extend the ProcessHandler?

Cheers, Andreas

archie18 avatar Oct 09 '14 21:10 archie18

Hi, Yes I think you can use two ways for this: 1: check by roles of the user https://github.com/lexik/LexikWorkflowBundle#workflow-example 2: listen on pre_validation event in this format:

`<process_name>.<current_step_name>.<next_step>.pre_validation` and 
`<process_name>.<current_step_name>.<next_step>.pre_validation_fail`

Before to reach the next step you can listen events and add violations and stay on the current step. Example :

Imagine you have a process_name: "post_publication", a current step "draft_created" and a next_state "validate".

So, you would like to check if you can reach the next step "validate". You need to listen on : "post_publication.draft_created.validate.pre_validation".

And here you can add a violation for fail the validation.

<?php
// your EventSubscriberInterface
    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return [
            'post_publication.draft_created.validate.pre_validation'      => 'onValidateDraft',
            'post_publication.draft_created.validate.pre_validation_fail' => 'onValidateDraftFail',
          ]
      }

    public function onValidateDraft(ValidateStepEvent $event)
    {
        /** @var Post $post */
        $post = $event->getModel();

        if ($userNotAllowed()) {
            // The step will stay in its current status
            $event->addViolation(
                $this->translator->trans('blog.user.flash_error', [], 'BlogBundle')
            );  
        }

        //...
    }

    public function onValidateDraftFail(StepEvent $event)
    {
        $modelState = $event->getModelState();

        // Do something with the modelState: logs, emails etc...
    }
}

May be you might be interested in this section also provide a history of the workflow : https://github.com/lexik/LexikWorkflowBundle#set-modelstates-on-your-modelinterface

gilles-g avatar Oct 15 '14 18:10 gilles-g

Hi, I'm using this project now to implement a workflow in my app. I have exactly the same issue, I want to render a botton to advance de workflow, obviously I only want to show this option to users that actually can advance the process.

asgarciap avatar Mar 02 '16 06:03 asgarciap

Hello, I'm interested in use LexikWorkflowBundle in my Company Project, but I dont know if i can use it together Symfony 3.2.

I know WorkflowBundle too, but after test the example of 2016 at Paris, and take a look at of documentation in symfony web site, this, is not enough to begin a production proyect

Could you help me?

Thanks

ghost avatar Feb 04 '17 22:02 ghost