BUGFIX: Allow creation of a tab and a field of the same name; bug cause by the duplicate field merging code introduced recently.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@62320 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-09-12 04:42:24 +00:00
parent 8eca8c3d7f
commit 6e5b511c46
4 changed files with 45 additions and 10 deletions

View File

@ -158,8 +158,19 @@ class CompositeField extends FormField {
public function insertBeforeRecursive($field, $insertBefore, $level = 0) {
return $this->children->insertBeforeRecursive($field, $insertBefore, $level+1);
}
public function removeByName($fieldName) {
$this->children->removeByName($fieldName);
/**
* Remove a field from this CompositeField by Name.
* The field could also be inside a CompositeField.
*
* @param string $fieldName The name of the field
* @param boolean $dataFieldOnly If this is true, then a field will only
* be removed if it's a data field. Dataless fields, such as tabs, will
* be left as-is.
*/
public function removeByName($fieldName, $dataFieldOnly = false) {
$this->children->removeByName($fieldName, $dataFieldOnly);
}
public function replaceField($fieldName, $newField) {

View File

@ -149,14 +149,19 @@ class FieldSet extends DataObjectSet {
* The field could also be inside a CompositeField.
*
* @param string $fieldName The name of the field
* @param boolean $dataFieldOnly If this is true, then a field will only
* be removed if it's a data field. Dataless fields, such as tabs, will
* be left as-is.
*/
public function removeByName($fieldName) {
public function removeByName($fieldName, $dataFieldOnly = false) {
foreach($this->items as $i => $child) {
if(is_object($child) && ($child->Name() == $fieldName || $child->Title() == $fieldName)) {
if(is_object($child) && ($child->Name() == $fieldName || $child->Title() == $fieldName) && (!$dataFieldOnly || $child->hasData())) {
if($child->class == 'Tab' && !$dataFieldOnly) Debug::backtrace();
array_splice( $this->items, $i, 1 );
break;
} else if($child->isComposite()) $child->removeByName($fieldName);
} else if($child->isComposite()) {
$child->removeByName($fieldName, $dataFieldOnly);
}
}
}
@ -329,7 +334,7 @@ class FieldSet extends DataObjectSet {
*/
function beforeInsert($item) {
if($this->sequentialSet) $this->sequentialSet = null;
$this->rootFieldSet()->removeByName($item->Name());
$this->rootFieldSet()->removeByName($item->Name(), true);
}

View File

@ -10,6 +10,7 @@ class TabSet extends CompositeField {
public function __construct($id) {
$tabs = func_get_args();
$this->id = array_shift($tabs);
$this->name = $this->id;
$this->title = $this->id;
foreach($tabs as $tab) $tab->setTabSet($this);
@ -81,8 +82,8 @@ class TabSet extends CompositeField {
return $level;
}
public function removeByName( $tabName ) {
parent::removeByName( $tabName );
public function removeByName( $tabName, $dataFieldOnly = false ) {
parent::removeByName( $tabName, $dataFieldOnly );
}
}
?>

View File

@ -344,10 +344,28 @@ class FieldSetTest extends SapphireTest {
)
)
);
/* Add a field to a non-existent tab, and it will be created */
$fieldSet->addFieldToTab("Root.Other", $b = new TextField("B"));
$this->assertNotNull($fieldSet->fieldByName('Root')->fieldByName('Other'));
$this->assertSame($b, $fieldSet->fieldByName('Root')->fieldByName('Other')->Fields()->First());
}
function testAddingFieldToATabWithTheSameNameAsTheField() {
$fieldSet = new FieldSet(
$root = new TabSet("Root",
$main = new Tab("Main",
$a = new TextField("A")
)
)
);
/* If you have a tab with the same name as the field, then technically it's a duplicate. However, it's allowed because
tab isn't a data field. Only duplicate data fields are problematic */
$fieldSet->addFieldToTab("Root.MyName", $myName = new TextField("MyName"));
$this->assertNotNull($fieldSet->fieldByName('Root')->fieldByName('MyName'));
$this->assertSame($myName, $fieldSet->fieldByName('Root')->fieldByName('MyName')->Fields()->First());
}
/**
* @TODO the same as above with insertBefore() and insertAfter()