diff --git a/forms/CheckboxSetField.php b/forms/CheckboxSetField.php
index 55eb8cf2b..f1ef4bbaa 100644
--- a/forms/CheckboxSetField.php
+++ b/forms/CheckboxSetField.php
@@ -132,11 +132,14 @@ class CheckboxSetField extends OptionsetField {
}
foreach($source as $value => $item) {
+ // Ensure $title is cast for template
if($item instanceof DataObject) {
$value = $item->ID;
- $title = $item->Title;
- } else {
+ $title = $item->obj('Title');
+ } elseif ($item instanceof DBField) {
$title = $item;
+ } else {
+ $title = DBField::create_field('Text', $item);
}
$itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value);
diff --git a/forms/OptionsetField.php b/forms/OptionsetField.php
index fb37c075b..9d9aca57b 100644
--- a/forms/OptionsetField.php
+++ b/forms/OptionsetField.php
@@ -62,6 +62,11 @@ class OptionsetField extends DropdownField {
if($source) {
foreach($source as $value => $title) {
+ // Ensure $title is safely cast
+ if ( !($title instanceof DBField) ) {
+ $title = DBField::create_field('Text', $title);
+ }
+
$itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value);
$odd = ($odd + 1) % 2;
$extraClass = $odd ? 'odd' : 'even';
diff --git a/templates/forms/CheckboxSetField.ss b/templates/forms/CheckboxSetField.ss
index f5210733e..1cf6c1195 100644
--- a/templates/forms/CheckboxSetField.ss
+++ b/templates/forms/CheckboxSetField.ss
@@ -2,9 +2,9 @@
<% if $Options.Count %>
<% loop $Options %>
- checked="checked"<% end_if %><% if $isDisabled %> disabled="disabled"<% end_if %> />
+ checked="checked"<% end_if %><% if $isDisabled %> disabled="disabled"<% end_if %> />
-
+
<% end_loop %>
<% else %>
No options available
diff --git a/templates/forms/OptionsetField.ss b/templates/forms/OptionsetField.ss
index aa3f4cc5e..e01272857 100644
--- a/templates/forms/OptionsetField.ss
+++ b/templates/forms/OptionsetField.ss
@@ -1,7 +1,7 @@
<% loop $Options %>
-
- checked<% end_if %><% if $isDisabled %> disabled<% end_if %> />
+ checked<% end_if %><% if $isDisabled %> disabled<% end_if %> />
<% end_loop %>
diff --git a/tests/forms/CheckboxSetFieldTest.php b/tests/forms/CheckboxSetFieldTest.php
index f22f2a866..94cb4e171 100644
--- a/tests/forms/CheckboxSetFieldTest.php
+++ b/tests/forms/CheckboxSetFieldTest.php
@@ -206,6 +206,27 @@ class CheckboxSetFieldTest extends SapphireTest {
);
}
+ public function testSafelyCast() {
+ $member = new Member();
+ $member->FirstName = '';
+ $member->Surname = '';
+ $member->write();
+ $field1 = new CheckboxSetField('Options', 'Options', array(
+ 'one' => 'One',
+ 'two' => 'Two & Three',
+ 'three' => DBField::create_field('HTMLText', 'Four & Five & Six'),
+ $member
+ ));
+ $fieldHTML = (string)$field1->Field();
+ $this->assertContains('One', $fieldHTML);
+ $this->assertContains('Two & Three', $fieldHTML);
+ $this->assertNotContains('Two & Three', $fieldHTML);
+ $this->assertContains('Four & Five & Six', $fieldHTML);
+ $this->assertNotContains('Four & Five & Six', $fieldHTML);
+ $this->assertContains('<firstname>', $fieldHTML);
+ $this->assertNotContains('', $fieldHTML);
+ }
+
}
/**
diff --git a/tests/forms/OptionsetFieldTest.php b/tests/forms/OptionsetFieldTest.php
index d07f698af..da7237086 100644
--- a/tests/forms/OptionsetFieldTest.php
+++ b/tests/forms/OptionsetFieldTest.php
@@ -63,4 +63,18 @@ class OptionsetFieldTest extends SapphireTest {
preg_match('/Yes/', $field->Field(), $matches);
$this->assertEquals($matches[0], 'Yes');
}
+
+ public function testSafelyCast() {
+ $field1 = new OptionsetField('Options', 'Options', array(
+ 1 => 'One',
+ 2 => 'Two & Three',
+ 3 => DBField::create_field('HTMLText', 'Four & Five & Six')
+ ));
+ $fieldHTML = (string)$field1->Field();
+ $this->assertContains('One', $fieldHTML);
+ $this->assertContains('Two & Three', $fieldHTML);
+ $this->assertNotContains('Two & Three', $fieldHTML);
+ $this->assertContains('Four & Five & Six', $fieldHTML);
+ $this->assertNotContains('Four & Five & Six', $fieldHTML);
+ }
}