raoul2000 / yii2-workflow

A simple workflow engine for Yii2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unsure how to recheck a Workflow rule on value change

bwragg opened this issue · comments

Hi,

This probably isn't a bug, probably more of a dumb user question, sorry if this isn't the place to post...been reading http://raoul2000.github.io/yii2-workflow/overview/ and playing with setting a rule like this:

['teststring','required',
        'on' => 'from {myworkflowid/draft} to {myworkflowid/correction}'     
        ]

This works great, it throws an error when 'teststring' is null and I try to change the status to correction.

But the issue I have is that AFTER i have added something into teststring and successfully changed to a correction status I can then re-edit the teststring field, delete the value setting it back to null and it allows me to save with no error.

Is there some way to deal with this? either role it back to a draft or prevent them from updating, e.g recheck the rule on update that has no status change. I suspect the issue is:
'on' => 'from {myworkflowid/draft} to {myworkflowid/correction}'

only runs when a status is actually transitioning but I can't seem to see another WorkflowValidator that seems to cover both scenarios. http://raoul2000.github.io/yii2-workflow/concept-events/ doesn't list any that seem to apply.

So it leaves me wondering if I have to sort of duplicate the rule/scenario? e.g have something like:


public function rules(){
  return [
  ['teststring','required',
     'on' => 'from {myworkflowid/draft} to {myworkflowid/correction}' 
   ],
   ['teststring','required','on' =>'myworkflowid/correction']
   ]
}

Thanks

Ben

commented

Hi @bwragg,
there is no dumb question, so don't worry 😉
You are totally right , the validation is only performed when the model changes status (i.e in a transition from source to target status). After that, we go back to "regular" validation following Yii2 rules. To follow up with your example, the fact that teststring is required in the transition from draft to correction is a priori not related to the fact that a model in status correction should have a value assigned to 'teststring' ... (even if it sound logical in this case).

By design, workflow driven model valiation only occurs when model changes status.

My advice would be to add a validation making use of the when parameter. For example :

public function rules(){
  return [
  ['teststring','required',
     'on' => 'from {myworkflowid/draft} to {myworkflowid/correction}' 
   ],
   ['teststring','required','when' => function($model) {
       return $model->status === 'myworkflowid/correction';
   }]
   ]
}

Hope this helps.
😎