Browse files

ENHANCEMENT Added OptionsetField->setDisabledItems() to allow specifi…

…cally disabling certain checkboxes

ENHANCEMENT Added CheckboxSetField->setDefaultItems() to tick specified checkboxes regardless of the value passed

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@99596 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent 4a93c9f commit 93895a4f0fe6c52fae1d316d372efdbfe96946a1 @chillu chillu committed Feb 22, 2010
Showing with 114 additions and 6 deletions.
  1. +27 −4 forms/CheckboxSetField.php
  2. +22 −0 forms/OptionsetField.php
  3. +38 −2 tests/forms/CheckboxSetFieldTest.php
  4. +27 −0 tests/forms/OptionsetFieldTest.php
View
31 forms/CheckboxSetField.php
@@ -21,6 +21,11 @@ class CheckboxSetField extends OptionsetField {
protected $disabled = false;
/**
+ * @var Array
+ */
+ protected $defaultItems = array();
+
+ /**
* @todo Explain different source data that can be used with this field,
* e.g. SQLMap, DataObjectSet or an array.
*
@@ -31,7 +36,7 @@ function Field() {
$source = $this->source;
$values = $this->value;
-
+
// Get values from the join, if available
if(is_object($this->form)) {
$record = $this->form->getRecord();
@@ -101,10 +106,10 @@ function Field() {
$checked = '';
if(isset($items)) {
- $checked = (in_array($key, $items)) ? ' checked="checked"' : '';
+ $checked = (in_array($key, $items) || in_array($key, $this->defaultItems)) ? ' checked="checked"' : '';
}
-
- $disabled = ($this->disabled) ? $disabled = ' disabled="disabled"' : '';
+
+ $disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? $disabled = ' disabled="disabled"' : '';
$options .= "<li class=\"$extraClass\"><input id=\"$itemID\" name=\"$this->name[$key]\" type=\"checkbox\" value=\"$key\"$checked $disabled class=\"checkbox\" /> <label for=\"$itemID\">$value</label></li>\n";
}
@@ -116,6 +121,24 @@ function setDisabled($val) {
}
/**
+ * Default selections, regardless of the {@link setValue()} settings.
+ * Note: Items marked as disabled through {@link setDisabledItems()} can still be
+ * selected by default through this method.
+ *
+ * @param Array $items Collection of array keys, as defined in the $source array
+ */
+ function setDefaultItems($items) {
+ $this->defaultItems = $items;
+ }
+
+ /**
+ * @return Array
+ */
+ function getDefaultItems() {
+ return $this->defaultItems;
+ }
+
+ /**
* Load a value into this CheckboxSetField
*/
function setValue($value, $obj = null) {
View
22 forms/OptionsetField.php
@@ -8,6 +8,11 @@
class OptionsetField extends DropdownField {
/**
+ * @var Array
+ */
+ protected $disabledItems = array();
+
+ /**
* Creates a new optionset field.
* @param name The field name
* @param title The field title
@@ -64,6 +69,23 @@ function performReadonlyTransformation() {
return $field;
}
+ /**
+ * Mark certain elements as disabled,
+ * regardless of the {@link setDisabled()} settings.
+ *
+ * @param array $items Collection of array keys, as defined in the $source array
+ */
+ function setDisabledItems($items) {
+ $this->disabledItems = $items;
+ }
+
+ /**
+ * @return Array
+ */
+ function getDisabledItems() {
+ return $this->disabledItems;
+ }
+
function ExtraOptions() {
return new DataObjectSet();
}
View
40 tests/forms/CheckboxSetFieldTest.php
@@ -7,6 +7,42 @@ class CheckboxSetFieldTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/forms/CheckboxSetFieldTest.yml';
+ function testSetDefaultItems() {
+ $f = new CheckboxSetField(
+ 'Test',
+ false,
+ array(0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => 'Three')
+ );
+
+ $f->setValue(array(0,1));
+ $f->setDefaultItems(array(2));
+ $p = new CSSContentParser($f->Field());
+ $item0 = $p->getBySelector('#Test_0');
+ $item1 = $p->getBySelector('#Test_1');
+ $item2 = $p->getBySelector('#Test_2');
+ $item3 = $p->getBySelector('#Test_3');
+ $this->assertEquals(
+ (string)$item0[0]['checked'],
+ 'checked',
+ 'Selected through value'
+ );
+ $this->assertEquals(
+ (string)$item1[0]['checked'],
+ 'checked',
+ 'Selected through value'
+ );
+ $this->assertEquals(
+ (string)$item2[0]['checked'],
+ 'checked',
+ 'Selected through default items'
+ );
+ $this->assertEquals(
+ (string)$item3[0]['checked'],
+ '',
+ 'Not selected by either value or default items'
+ );
+ }
+
function testAddExtraClass() {
/* CheckboxSetField has an extra class name and is in the HTML the field returns */
$cboxSetField = new CheckboxSetField('FeelingOk', 'Are you feeling ok?', array(0 => 'No', 1 => 'Yes'), '', null, '(Select one)');
@@ -32,7 +68,7 @@ function testSaveWithNothingSelected() {
'Nothing should go into manymany join table for a saved field without any ticked boxes'
);
}
-
+
function testSaveWithArrayValueSet() {
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
$articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
@@ -72,7 +108,7 @@ function testLoadDataFromObject() {
$articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
$tag2 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag2');
-
+
$field = new CheckboxSetField("Tags", "Test field", DataObject::get("CheckboxSetFieldTest_Tag")->map());
$form = new Form(
new Controller(),
View
27 tests/forms/OptionsetFieldTest.php
@@ -0,0 +1,27 @@
+<?php
+/**
+ * @package sapphire
+ * @subpackage tests
+ */
+class OptionsetFieldTest extends SapphireTest {
+ function testSetDisabledItems() {
+ $f = new OptionsetField(
+ 'Test',
+ false,
+ array(0 => 'Zero', 1 => 'One')
+ );
+
+ $f->setDisabledItems(array(0));
+ $p = new CSSContentParser($f->Field());
+ $item0 = $p->getBySelector('#Test_0');
+ $item1 = $p->getBySelector('#Test_1');
+ $this->assertEquals(
+ (string)$item0[0]['disabled'],
+ 'disabled'
+ );
+ $this->assertEquals(
+ (string)$item1[0]['disabled'],
+ ''
+ );
+ }
+}

0 comments on commit 93895a4

Please sign in to comment.