diff --git a/code/formfields/UserFormsFieldList.php b/code/formfields/UserFormsFieldList.php index 4f41e58..f01a3f6 100644 --- a/code/formfields/UserFormsFieldList.php +++ b/code/formfields/UserFormsFieldList.php @@ -29,4 +29,15 @@ class UserFormsFieldList extends FieldList implements UserFormsFieldContainer { return $this; } + /** + * Remove all empty steps + */ + public function clearEmptySteps() { + foreach($this as $field) { + if($field instanceof UserFormsStepField && count($field->getChildren()) === 0) { + $this->remove($field); + } + } + } + } diff --git a/code/forms/UserForm.php b/code/forms/UserForm.php index 8477442..1f8db53 100644 --- a/code/forms/UserForm.php +++ b/code/forms/UserForm.php @@ -83,6 +83,7 @@ class UserForm extends Form { foreach ($this->controller->Fields() as $field) { $target = $target->processNext($field); } + $fields->clearEmptySteps(); $this->extend('updateFormFields', $fields); return $fields; } diff --git a/tests/UserDefinedFormTest.yml b/tests/UserDefinedFormTest.yml index a8fba3c..9decd15 100644 --- a/tests/UserDefinedFormTest.yml +++ b/tests/UserDefinedFormTest.yml @@ -7,6 +7,12 @@ EditableFormStep: Title: 'Step 1' form5step1: Title: 'Step 1' + form6step1: + Title: 'Step 1' + form6step2: + Title: 'Step 2' + form6step3: + Title: 'Step 2' EditableOption: option-1: @@ -84,6 +90,12 @@ EditableTextField: CustomErrorMessage: Custom Error Message Required: true + field-1: + Name: Field1 + + field-2: + Name: Field2 + EditableDropdown: basic-dropdown: Name: basic-dropdown @@ -215,3 +227,7 @@ UserDefinedForm: Title: 'Page with filtered recipients' Fields: =>EditableFormStep.form5step1,=>EditableCheckboxGroupField.checkbox-group, =>EditableTextField.your-name-field, =>EditableTextField.street-field, =>EditableTextField.city-field EmailRecipients: =>UserDefinedForm_EmailRecipient.unfiltered-recipient-1, =>UserDefinedForm_EmailRecipient.filtered-recipient-1, =>UserDefinedForm_EmailRecipient.filtered-recipient-2 + + empty-page: + Title: 'Page with empty step' + Fields: =>EditableFormStep.form6step1, =>EditableTextField.field-1, =>EditableFormStep.form6step2, =>EditableTextField.field-2, =>EditableFormStep.form6step3 diff --git a/tests/UserFormTest.php b/tests/UserFormTest.php new file mode 100644 index 0000000..5496748 --- /dev/null +++ b/tests/UserFormTest.php @@ -0,0 +1,18 @@ +objFromFixture('UserDefinedForm', 'empty-page'); + $this->assertEquals(5, $page->Fields()->count()); + $controller = ModelAsController::controller_for($page); + $form = new UserForm($controller); + $this->assertEquals(2, $form->getSteps()->count()); + } +} \ No newline at end of file