Merge pull request #9976 from kinglozzer/9975-default-form-action

FIX: Form::defaultAction() didn't work if actions were in CompositeFields (fixes #9975)
This commit is contained in:
Guy Sartorelli 2023-05-09 21:45:24 +12:00 committed by GitHub
commit 234e229098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -1645,7 +1645,9 @@ class Form extends ViewableData implements HasRequestHandler
public function defaultAction() public function defaultAction()
{ {
if ($this->hasDefaultAction && $this->actions) { if ($this->hasDefaultAction && $this->actions) {
return $this->actions->first(); return $this->actions->flattenFields()->filterByCallback(function ($field) {
return $field instanceof FormAction;
})->first();
} }
return null; return null;
} }

View File

@ -7,6 +7,7 @@ use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Dev\CSSContentParser; use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\DateField; use SilverStripe\Forms\DateField;
use SilverStripe\Forms\DatetimeField; use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\FieldList; 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\Player;
use SilverStripe\Forms\Tests\FormTest\Team; use SilverStripe\Forms\Tests\FormTest\Team;
use SilverStripe\Forms\Tests\FormTest\TestController; use SilverStripe\Forms\Tests\FormTest\TestController;
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator;
use SilverStripe\Forms\TextareaField; use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TimeField; use SilverStripe\Forms\TimeField;
@ -348,6 +348,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() public function testLoadDataFromIgnoreFalseish()
{ {
$form = new Form( $form = new Form(