MINOR Update CompositeField::__construct to call setChildren #8460

This commit is contained in:
Maxime Rainville 2018-10-11 15:25:34 +13:00
parent 98afbfe9b4
commit 250d925810
2 changed files with 25 additions and 9 deletions

View File

@ -53,16 +53,15 @@ class CompositeField extends FormField
public function __construct($children = null)
{
if ($children instanceof FieldList) {
$this->children = $children;
} elseif (is_array($children)) {
$this->children = new FieldList($children);
} else {
//filter out null/empty items
// Normalise $children to a FieldList
if (!$children instanceof FieldList) {
if (!is_array($children)) {
// Fields are provided as a list of arguments
$children = array_filter(func_get_args());
$this->children = new FieldList($children);
}
$this->children->setContainerField($this);
$children = new FieldList($children);
}
$this->setChildren($children);
parent::__construct(null, false);
}

View File

@ -4,6 +4,7 @@ namespace SilverStripe\Forms\Tests;
use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\DropdownField;
@ -107,4 +108,20 @@ class CompositeFieldTest extends SapphireTest
"Validates when children are valid"
);
}
public function testChildren()
{
$field = CompositeField::create();
$this->assertInstanceOf(FieldList::class, $field->getChildren());
$this->assertEquals($field, $field->getChildren()->getContainerField());
$expectedChildren = FieldList::create(
$fieldOne = DropdownField::create('A', '', [ 'value' => 'value' ]),
$fieldTwo = TextField::create('B')
);
$field->setChildren($expectedChildren);
$this->assertEquals($expectedChildren, $field->getChildren());
$this->assertEquals($field, $expectedChildren->getContainerField());
}
}