From ff7b0607aa4de3140f9ff938d258cad826ed09fe Mon Sep 17 00:00:00 2001 From: Lucas Yang Date: Sun, 16 Jun 2019 16:12:28 +0800 Subject: [PATCH] Fix back step no save data bug --- resources/views/base.blade.php | 36 ++++++++++++----------- src/Http/Controllers/WizardController.php | 24 ++++++++------- src/Step.php | 2 +- tests/Feature/HttpTest.php | 23 +++++++++++++-- 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/resources/views/base.blade.php b/resources/views/base.blade.php index a4cd892..72761b0 100644 --- a/resources/views/base.blade.php +++ b/resources/views/base.blade.php @@ -32,24 +32,26 @@ @include($step->view(), compact('step', 'errors')) -
- @if ($stepRepo->hasPrev()) - - @lang('wizard::generic.back') - - @else - - @endif +
+
+ @if ($stepRepo->hasPrev()) + + @endif +
- @if ($stepRepo->hasNext()) - - @else - - @endif +
+ @if ($stepRepo->hasNext()) + + @else + + @endif +
diff --git a/src/Http/Controllers/WizardController.php b/src/Http/Controllers/WizardController.php index f2b3ef6..f0c6d4a 100644 --- a/src/Http/Controllers/WizardController.php +++ b/src/Http/Controllers/WizardController.php @@ -95,13 +95,6 @@ public function create(Request $request, $step = null) // Check this step is not last processed step. if ($step->index() !== $lastProcessedIndex) { - - // If trigger from 'back', - // Set this step index and redirect to this step. - if ($request->query('trigger') === 'back') { - return $this->setThisStepAndRedirectTo($request, $step); - } - // Redirect to last processed step. return $this->redirectToLastProcessedStep( $request, @@ -137,6 +130,13 @@ public function store(Request $request, string $step) $step->saveData($request, $step->getRequestData($request), $step->model()); } + // If trigger from 'back', + // Set this step index and redirect to prev step. + if ($request->query('_trigger') === 'back') { + $prevStep = $this->wizard()->stepRepo()->prev(); + return $this->setThisStepAndRedirectTo($request, $prevStep); + } + if (!$this->getNextStepSlug()) { // Wizard done... if ($this->wizard()->option('cache')) { @@ -174,10 +174,12 @@ public function done(Request $request) */ protected function setThisStepAndRedirectTo(Request $request, Step $step) { - $this->wizard()->cacheStepData( - $this->wizard()->cache()->get(), - $step->index() - ); + if ($this->wizard()->option('cache')) { + $this->wizard()->cacheStepData( + $this->wizard()->cache()->get(), + $step->index() + ); + } return redirect()->route( $request->route()->getName(), diff --git a/src/Step.php b/src/Step.php index fd2de5a..73a177c 100644 --- a/src/Step.php +++ b/src/Step.php @@ -53,7 +53,7 @@ abstract class Step * * @var array */ - protected $inputExcept = ['_token', '_method']; + protected $inputExcept = ['_token', '_method', '_trigger']; /** * Create a new step instance. diff --git a/tests/Feature/HttpTest.php b/tests/Feature/HttpTest.php index b7882fa..56513ba 100644 --- a/tests/Feature/HttpTest.php +++ b/tests/Feature/HttpTest.php @@ -84,7 +84,7 @@ public function testRunAllWizardSteps() $this->assertDatabaseHas('users', [ 'name' => 'John', ]); - $this->assertDatabaseHas('posts',[ + $this->assertDatabaseHas('posts', [ 'title' => 'Title', 'content' => 'Content.', ]); @@ -108,16 +108,33 @@ public function testWizardStepNotEquialToLastProcessedStep() $response->assertRedirect('/wizard/test/post-step-stub'); } - public function testWizardStepTriggerFromBack() + public function testWizardStepTriggerToBack() { $this->session([ 'laravel_wizard.test' => [ + 'user-step-stub' => [ + 'name' => 'John', + ], '_last_index' => 1, ], ]); - $response = $this->get('/wizard/test/user-step-stub?trigger=back'); + $response = $this->post('/wizard/test/post-step-stub?_trigger=back', [ + 'title' => 'Title', + 'content' => 'Content.', + ]); $response->assertRedirect('/wizard/test/user-step-stub'); + + $this->assertEquals([ + 'user-step-stub' => [ + 'name' => 'John', + ], + 'post-step-stub' => [ + 'title' => 'Title', + 'content' => 'Content.', + ], + '_last_index' => 0, + ], $this->app['session']->get('laravel_wizard.test')); } public function testWizardCacheDatabaseDriver()