Skip to content

Commit

Permalink
FIX: Form::defaultAction() didn't work if actions were in CompositeFi…
Browse files Browse the repository at this point in the history
…elds (fixes silverstripe#9975)
  • Loading branch information
kinglozzer committed Jun 9, 2021
1 parent d5e4493 commit 5bb5ef8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,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;
}
Expand Down
19 changes: 18 additions & 1 deletion tests/php/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -338,6 +338,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(
Expand Down

0 comments on commit 5bb5ef8

Please sign in to comment.