Skip to content

Commit

Permalink
Merge pull request #24 from bayareawebpro/dev
Browse files Browse the repository at this point in the history
Before Callbacks Bugfix
  • Loading branch information
bayareawebpro committed Jul 9, 2020
2 parents bbab505 + 3b6e8df commit 83d078e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ Set a field value from the session form state.

#### `currentStep()`

Get the current step number.
Get the current saved step number.

#### `requestedStep()`

Get the requested step number.

#### `isStep(int $step = 1)`

Expand Down
19 changes: 14 additions & 5 deletions src/MultiStepForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function handleRequest()

if ($response = (
$this->handleBefore('*') ??
$this->handleBefore($this->currentStep())
$this->handleBefore($this->requestedStep())
)) {
return $response;
}
Expand Down Expand Up @@ -149,7 +149,7 @@ protected function getData(array $merge = []): array
*/
protected function validate(): array
{
$step = $this->stepConfig((int)$this->request->get('form_step', 1));
$step = $this->stepConfig($this->requestedStep());

return $this->request->validate(
array_merge($step->get('rules', []), [
Expand Down Expand Up @@ -193,7 +193,7 @@ protected function shouldNavigateBack(): bool
$this->request->isMethod('GET') &&
$this->request->filled('form_step')
) {
$step = (int) $this->request->get('form_step', 1);
$step = $this->requestedStep();
if ($this->steps->has($step) && $this->isPast($step)) {
$this->setValue('form_step', $step);
}
Expand Down Expand Up @@ -361,6 +361,15 @@ public function currentStep(): int
return (int)$this->session->get("{$this->namespace}.form_step", 1);
}

/**
* Get Requested Step
* @return int
*/
public function requestedStep(): int
{
return (int)$this->request->get("form_step", 1);
}

/**
* Get the current step config or by number.
* @param int $step
Expand Down Expand Up @@ -411,7 +420,7 @@ public function setValue(string $key, $value): self
protected function nextStep(): self
{
if (!$this->wasReset && !$this->isStep($this->lastStep())) {
$this->setValue('form_step', 1 + (int) $this->request->get('form_step', 1));
$this->setValue('form_step', 1 + $this->requestedStep());
}
return $this;
}
Expand All @@ -422,7 +431,7 @@ protected function nextStep(): self
*/
public function lastStep(): int
{
return $this->steps->keys()->max(fn($value) => $value) ?? 1;
return $this->steps->keys()->filter(fn($value) => is_int($value))->max() ?? 1;
}

/**
Expand Down
22 changes: 17 additions & 5 deletions tests/Fixtures/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,33 @@
return response('before*');
}
})
->beforeStep(1, function (MultiStepForm $form) {
if($form->request->filled('before1')){
return response('before1');
}
})
->onStep('*', function (MultiStepForm $form) {
if($form->request->filled('on*')){
return response('on*');
}
})
->addStep(1)
->onStep(1, function (MultiStepForm $form) {
if($form->request->filled('on1')){
return response('on1');
}
})
->beforeStep(1, function (MultiStepForm $form) {
if($form->request->filled('before1')){
return response('before1');
}
})
->addStep(2)
->onStep(2, function (MultiStepForm $form) {
if($form->request->filled('on2')){
return response('on2');
}
})
->beforeStep(2, function (MultiStepForm $form) {
if($form->request->filled('before2')){
return response('before2');
}
})
;
})
->middleware('web')
Expand Down
30 changes: 23 additions & 7 deletions tests/Unit/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function test_wildcard_on()
->json('POST', route('hooks'),[
'on*' =>true,
'form_step' =>1,
'name' =>'name',
])
->assertSee('on*')
->assertOk()
Expand All @@ -44,25 +43,42 @@ public function test_wildcard_on()
public function test_step_before()
{
$this->startSession();

$this
->json('POST', route('hooks'),[
'form_step' => 1,
'on1' =>true,
])
->assertDontSee('before1')
->assertSee('on1')
->assertOk()
;
$this
->json('POST', route('hooks'),[
'before1' =>true,
'form_step' => 1
])
->assertSee('before1')
->assertOk()
;
}

public function test_step_on()
public function test_step_before2()
{
$this->startSession();
$this->withSession(['test' => ['form_step'=>1]]);
$this
->json('POST', route('hooks'),[
'on1' =>true,
'form_step' =>1,
'name' =>'name',
'form_step' => 2
])
->assertSee('on1')
->assertDontSee('before2')
->assertOk()
;
$this
->json('POST', route('hooks'),[
'before2' =>true,
'form_step' => 2
])
->assertSee('before2')
->assertOk()
;
}
Expand Down

0 comments on commit 83d078e

Please sign in to comment.