Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional persistence of step data on flow back transition #248

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
54 changes: 54 additions & 0 deletions Form/FormFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ abstract class FormFlow implements FormFlowInterface {
*/
protected $revalidatePreviousSteps = true;

/**
* @var boolean If this is set to true then step data for the current step will be saved when transitioning backwards.
*/
protected $persistOnBackTransition = false;

/**
* @var boolean
*/
Expand Down Expand Up @@ -347,6 +352,17 @@ public function isRevalidatePreviousSteps() {
return $this->revalidatePreviousSteps;
}

public function setPersistOnBackTransition($persistOnBackTransition) {
$this->persistOnBackTransition = (boolean) $persistOnBackTransition;
}

/**
* {@inheritDoc}
*/
public function isPersistOnBackTransition() {
return $this->persistOnBackTransition;
}

public function setAllowDynamicStepNavigation($allowDynamicStepNavigation) {
$this->allowDynamicStepNavigation = (boolean) $allowDynamicStepNavigation;
}
Expand Down Expand Up @@ -494,6 +510,32 @@ public function nextStep() {

return false; // should never be reached, but just in case
}

/**
* {@inheritDoc}
*/
public function previousStep() {
$currentStepNumber = $this->currentStepNumber - 1;

foreach ($this->getSteps() as $step) {
$step->evaluateSkipping($currentStepNumber, $this);
}

// There is no "before" step as the target step precedes the first step.
if ($currentStepNumber < $this->getFirstStepNumber()) {
return false;
}

$currentStepNumber = $this->applySkipping($currentStepNumber);

if ($currentStepNumber <= $this->getStepCount()) {
$this->currentStepNumber = $currentStepNumber;

return true;
}

return false; // should never be reached, but just in case
}

/**
* {@inheritDoc}
Expand Down Expand Up @@ -667,6 +709,18 @@ protected function bindFlow() {

$this->currentStepNumber = $requestedStepNumber;

if ($this->persistOnBackTransition && $this->getRequestedTransition() === self::TRANSITION_BACK) {

/**
* If persistence on backwards transition is enabled and the current request transition is 'back' then
* persist the current step data to storage.
*/
$this->nextStep();
$form = $this->createFormForStep($this->getCurrentStepNumber());
$this->saveCurrentStepData($form);
$this->previousStep();
}

if (!$this->allowDynamicStepNavigation && $this->getRequestedTransition() === self::TRANSITION_BACK) {
/*
* Don't invalidate data for the current step to properly show the filled out form for that step after
Expand Down