mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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
This commit is contained in:
parent
4a93c9f6a7
commit
93895a4f0f
@ -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 .= "<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";
|
||||
}
|
||||
|
||||
@ -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
|
||||
*/
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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(),
|
||||
|
27
tests/forms/OptionsetFieldTest.php
Normal file
27
tests/forms/OptionsetFieldTest.php
Normal file
@ -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'],
|
||||
''
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user