diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 838781221c5..d4b039d6432 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -1645,7 +1645,9 @@ public function renderWithoutActionButton($template) public function defaultAction() { if ($this->hasDefaultAction && $this->actions) { - return $this->actions->first(); + return $this->actions->flattenFields()->filterByCallback(function ($field) { + return $field instanceof FormAction; + })->first(); } return null; } diff --git a/tests/php/Forms/FormTest.php b/tests/php/Forms/FormTest.php index 34689f4877c..16f32c290cc 100644 --- a/tests/php/Forms/FormTest.php +++ b/tests/php/Forms/FormTest.php @@ -7,6 +7,7 @@ use SilverStripe\Control\Session; use SilverStripe\Dev\CSSContentParser; use SilverStripe\Dev\FunctionalTest; +use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\DateField; use SilverStripe\Forms\DatetimeField; use SilverStripe\Forms\FieldList; @@ -23,7 +24,6 @@ use SilverStripe\Forms\Tests\FormTest\Player; use SilverStripe\Forms\Tests\FormTest\Team; use SilverStripe\Forms\Tests\FormTest\TestController; -use SilverStripe\Forms\Tests\ValidatorTest\TestValidator; use SilverStripe\Forms\TextareaField; use SilverStripe\Forms\TextField; use SilverStripe\Forms\TimeField; @@ -348,6 +348,23 @@ public function testLookupFieldDisabledSaving() ); } + public function testDefaultAction() + { + $form = Form::create(Controller::curr(), 'Form', new FieldList(), new FieldList( + new FormAction('doForm', 'Form Action') + )); + $this->assertNotNull($form->defaultAction()); + $this->assertEquals('action_doForm', $form->defaultAction()->getName()); + + $form = Form::create(Controller::curr(), 'AnotherForm', new FieldList(), new FieldList( + new CompositeField( + new FormAction('doAnotherForm', 'Another Form Action') + ) + )); + $this->assertNotNull($form->defaultAction()); + $this->assertEquals('action_doAnotherForm', $form->defaultAction()->getName()); + } + public function testLoadDataFromIgnoreFalseish() { $form = new Form(