From 5bb5ef80ede8d75f2de05c8aaf367925269a14de Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Wed, 9 Jun 2021 17:26:43 +0100 Subject: [PATCH] FIX: Form::defaultAction() didn't work if actions were in CompositeFields (fixes #9975) --- src/Forms/Form.php | 4 +++- tests/php/Forms/FormTest.php | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 1d3516954..5718b1f52 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -1682,7 +1682,9 @@ class Form extends ViewableData implements HasRequestHandler 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 b31a4a254..2b3b3a4ee 100644 --- a/tests/php/Forms/FormTest.php +++ b/tests/php/Forms/FormTest.php @@ -7,6 +7,7 @@ use SilverStripe\Control\HTTPRequest; 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\ControllerWithStrictPostCheck; 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; @@ -338,6 +338,23 @@ class FormTest extends FunctionalTest ); } + 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(