diff --git a/src/FormManager.php b/src/FormManager.php index 831be84..a3185e9 100644 --- a/src/FormManager.php +++ b/src/FormManager.php @@ -414,16 +414,21 @@ private function prepare(): void } $i = 0; + $isPageBreakLastFormField = false; foreach ($this->formFieldModels as $k => $formField) { $this->formFieldsPerStep[$i][$k] = $formField; if ($this->isPageBreak($formField)) { + $isPageBreakLastFormField = true; + // Set the name on the model, otherwise one has to enter it in the back end every time $formField->name = $formField->type; // Increase counter ++$i; + } else { + $isPageBreakLastFormField = false; } // If we have a regular submit form field, that's a misconfiguration @@ -432,6 +437,20 @@ private function prepare(): void } } + // If the last form field is not a page break, we need to merge the last step into the previous one + if (!$isPageBreakLastFormField) { + $lastStepIndex = count($this->formFieldsPerStep) - 1; + + if (isset($this->formFieldsPerStep[$lastStepIndex], $this->formFieldsPerStep[$lastStepIndex - 1])) { + $this->formFieldsPerStep[$lastStepIndex - 1] = [ + ...$this->formFieldsPerStep[$lastStepIndex - 1], // Second last step + ...$this->formFieldsPerStep[$lastStepIndex], // Last step + ]; + + array_pop($this->formFieldsPerStep); + } + } + $this->prepared = true; $this->preparing = false; }