Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

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

Open
archie18 opened this issue Oct 9, 2014 · 3 comments
Open

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

archie18 opened this issue Oct 9, 2014 · 3 comments

Comments

@archie18
Copy link

archie18 commented Oct 9, 2014

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

@gilles-g
Copy link
Member

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

@asgarciap
Copy link

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.

@ghost
Copy link

ghost commented Feb 4, 2017

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants