BUGFIX Fix disappearing fields when a field without a name was being pushed onto a FieldSet (eg a CompositeField)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64263 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Andrew O'Neil 2008-10-15 01:30:14 +00:00
parent f58014490c
commit 68aaa32e86
2 changed files with 15 additions and 3 deletions

View File

@ -163,6 +163,10 @@ class FieldSet extends DataObjectSet {
* be left as-is.
*/
public function removeByName($fieldName, $dataFieldOnly = false) {
if(!$fieldName) {
user_error('FieldSet::removeByName() was called with a blank field name.', E_USER_WARNING);
}
foreach($this->items as $i => $child) {
if(is_object($child) && ($child->Name() == $fieldName || $child->Title() == $fieldName) && (!$dataFieldOnly || $child->hasData())) {
//if($child->class == 'Tab' && !$dataFieldOnly) Debug::backtrace();
@ -385,7 +389,7 @@ class FieldSet extends DataObjectSet {
*/
function beforeInsert($item) {
if($this->sequentialSet) $this->sequentialSet = null;
$this->rootFieldSet()->removeByName($item->Name(), true);
if($item->Name()) $this->rootFieldSet()->removeByName($item->Name(), true);
}
@ -524,4 +528,4 @@ class FieldSet extends DataObjectSet {
}
?>
?>

View File

@ -281,6 +281,14 @@ class FieldSetTest extends SapphireTest {
/* There are now 2 fields in the set */
$this->assertEquals(2, $fields->Count());
// Test that pushing a composite field without a name onto the set works
// See ticket #2932
$fields->push(new CompositeField(
new TextField('Test1'),
new TextField('Test2')
));
$this->assertEquals(3, $fields->Count());
}
/**
@ -439,4 +447,4 @@ class FieldSetTest extends SapphireTest {
}
?>
?>