ycs77 / laravel-wizard

A web Setup Wizard for Laravel application.
MIT License
121 stars 18 forks source link

How to intercept back button when pressed? #49

Closed mrizkihidayat66 closed 1 month ago

mrizkihidayat66 commented 1 month ago
class ProductStep extends Step
{
    // ...

    public function prev()
    {
        // do something before back to prev step, for example clear current saved data

        parent::prev();
    }
ycs77 commented 1 month ago

Hi @mrizkihidayat66, you can call the beforeBack() method of the Step class in beforeBackWizardStep() hook of the WizardController:

use Illuminate\Http\Request;

class UserWizardController extends Controller
{
    protected function beforeBackWizardStep(Request $request)
    {
        /** @var mixed $step */
        $step = $this->wizard()->stepRepo()->current();

        if (method_exists($step, 'beforeBack')) {
            $step->beforeBack();
        }

        return true;
    }
}

Now you can add the beforeBack() method into your Step class:

class EmailStep extends Step
{
    public function beforeBack()
    {
        // do something
    }
}