From 93895a4f0fe6c52fae1d316d372efdbfe96946a1 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 22 Feb 2010 06:11:58 +0000 Subject: [PATCH] ENHANCEMENT Added OptionsetField->setDisabledItems() to allow specifically 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 --- forms/CheckboxSetField.php | 31 ++++++++++++++++++--- forms/OptionsetField.php | 22 +++++++++++++++ tests/forms/CheckboxSetFieldTest.php | 40 ++++++++++++++++++++++++++-- tests/forms/OptionsetFieldTest.php | 27 +++++++++++++++++++ 4 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 tests/forms/OptionsetFieldTest.php diff --git a/forms/CheckboxSetField.php b/forms/CheckboxSetField.php index 5cef1b055..58f543050 100755 --- a/forms/CheckboxSetField.php +++ b/forms/CheckboxSetField.php @@ -20,6 +20,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 @@ class CheckboxSetField extends OptionsetField { $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 @@ class CheckboxSetField extends OptionsetField { $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 .= "
  • name[$key]\" type=\"checkbox\" value=\"$key\"$checked $disabled class=\"checkbox\" />
  • \n"; } @@ -115,6 +120,24 @@ class CheckboxSetField extends OptionsetField { $this->disabled = $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 */ diff --git a/forms/OptionsetField.php b/forms/OptionsetField.php index 1bd8783b5..83a76c3a6 100755 --- a/forms/OptionsetField.php +++ b/forms/OptionsetField.php @@ -7,6 +7,11 @@ */ class OptionsetField extends DropdownField { + /** + * @var Array + */ + protected $disabledItems = array(); + /** * Creates a new optionset field. * @param name The field name @@ -64,6 +69,23 @@ class OptionsetField extends DropdownField { 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(); } diff --git a/tests/forms/CheckboxSetFieldTest.php b/tests/forms/CheckboxSetFieldTest.php index c3e10892e..d22e47d42 100644 --- a/tests/forms/CheckboxSetFieldTest.php +++ b/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 @@ class CheckboxSetFieldTest extends SapphireTest { '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 @@ class CheckboxSetFieldTest extends SapphireTest { $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(), diff --git a/tests/forms/OptionsetFieldTest.php b/tests/forms/OptionsetFieldTest.php new file mode 100644 index 000000000..113395454 --- /dev/null +++ b/tests/forms/OptionsetFieldTest.php @@ -0,0 +1,27 @@ + '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'], + '' + ); + } +} \ No newline at end of file