Skip to content

Commit

Permalink
ENHANCEMENT Added OptionsetField->setDisabledItems() to allow specifi…
Browse files Browse the repository at this point in the history
…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
chillu committed Feb 22, 2010
1 parent 4a93c9f commit 93895a4
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
31 changes: 27 additions & 4 deletions forms/CheckboxSetField.php
Expand Up @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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";
}

Expand All @@ -115,6 +120,24 @@ function setDisabled($val) {
$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
*/
Expand Down
22 changes: 22 additions & 0 deletions forms/OptionsetField.php
Expand Up @@ -7,6 +7,11 @@
*/
class OptionsetField extends DropdownField {

/**
* @var Array
*/
protected $disabledItems = array();

/**
* Creates a new optionset field.
* @param name The field name
Expand Down Expand Up @@ -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();
}
Expand Down
40 changes: 38 additions & 2 deletions tests/forms/CheckboxSetFieldTest.php
Expand Up @@ -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)');
Expand All @@ -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');
Expand Down Expand Up @@ -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(),
Expand Down
27 changes: 27 additions & 0 deletions 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.