Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

BUG Fixes serious issue with FieldList::addFieldsToTab failing to acc…

…ept multiple field groups.

Additional groups beyond the first are ignored.
Test cases included.
  • Loading branch information...
commit 24950692cd4f42fdb20ed029292d53dcaf5ec40c 1 parent 688d853
Damian Mooyman tractorcow authored
Showing with 38 additions and 1 deletion.
  1. +1 −1  forms/FieldList.php
  2. +37 −0 tests/forms/FieldListTest.php
2  forms/FieldList.php
View
@@ -130,7 +130,7 @@ public function addFieldsToTab($tabName, $fields, $insertBefore = null) {
// Check if a field by the same name exists in this tab
if($insertBefore) {
$tab->insertBefore($field, $insertBefore);
- } elseif($tab->fieldByName($field->getName())) {
+ } elseif(($name = $field->getName()) && $tab->fieldByName($name)) {
// It exists, so we need to replace the old one
$this->replaceField($field->getName(), $field);
} else {
37 tests/forms/FieldListTest.php
View
@@ -48,6 +48,43 @@ public function testAddFieldToTab() {
/* We'll have 3 fields inside the tab */
$this->assertEquals(3, $tab->Fields()->Count());
}
+
+ /**
+ * Test that groups can be added to a fieldlist
+ */
+ public function testFieldgroup() {
+ $fields = new FieldList();
+ $tab = new Tab('Root');
+ $fields->push($tab);
+
+ $fields->addFieldsToTab('Root', array(
+ $group1 = new FieldGroup(
+ new TextField('Name'),
+ new EmailField('Email')
+ ),
+ $group2 = new FieldGroup(
+ new TextField('Company'),
+ new TextareaField('Address')
+ )
+ ));
+
+ /* Check that the field objects were created */
+ $this->assertNotNull($fields->dataFieldByName('Name'));
+ $this->assertNotNull($fields->dataFieldByName('Email'));
+ $this->assertNotNull($fields->dataFieldByName('Company'));
+ $this->assertNotNull($fields->dataFieldByName('Address'));
+
+ /* The field objects in the set should be the same as the ones we created */
+ $this->assertSame($fields->dataFieldByName('Name'), $group1->fieldByName('Name'));
+ $this->assertSame($fields->dataFieldByName('Email'), $group1->fieldByName('Email'));
+ $this->assertSame($fields->dataFieldByName('Company'), $group2->fieldByName('Company'));
+ $this->assertSame($fields->dataFieldByName('Address'), $group2->fieldByName('Address'));
+
+ /* We'll have 2 fields directly inside the tab */
+ $this->assertEquals(2, $tab->Fields()->Count());
+
+
+ }
/**
* Test removing a single field from a tab in a set.
Please sign in to comment.
Something went wrong with that request. Please try again.