NEW Disable items in DropdownField and GroupedDropdownField

This commit is contained in:
micschk 2012-10-17 12:06:02 +03:00 committed by Ingo Schommer
parent f3c0669fec
commit 7f605cfb54
3 changed files with 128 additions and 3 deletions

View File

@ -66,6 +66,14 @@
* ); * );
* </code> * </code>
* *
* <b>Disabling individual items</b>
*
* Individual items can be disabled by feeding their array keys to setDisabledItems.
*
* <code>
* $DrDownField->setDisabledItems( array( 'US', 'GEM' ) );
* </code>
*
* @see CheckboxSetField for multiple selections through checkboxes instead. * @see CheckboxSetField for multiple selections through checkboxes instead.
* @see ListboxField for a single <select> box (with single or multiple selections). * @see ListboxField for a single <select> box (with single or multiple selections).
* @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements * @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
@ -103,6 +111,11 @@ class DropdownField extends FormField {
*/ */
protected $emptyString = ''; protected $emptyString = '';
/**
* @var array $disabledItems The keys for items that should be disabled (greyed out) in the dropdown
*/
protected $disabledItems = array();
/** /**
* Creates a new dropdown field. * Creates a new dropdown field.
* @param $name The field name * @param $name The field name
@ -154,11 +167,17 @@ class DropdownField extends FormField {
$selected = ($value) ? $value == $this->value : $value === $this->value; $selected = ($value) ? $value == $this->value : $value === $this->value;
$this->isSelected = $selected; $this->isSelected = $selected;
} }
$disabled = false;
if(in_array($value, $this->disabledItems) && $title != $this->emptyString ){
$disabled = 'disabled';
}
$options[] = new ArrayData(array( $options[] = new ArrayData(array(
'Title' => $title, 'Title' => $title,
'Value' => $value, 'Value' => $value,
'Selected' => $selected, 'Selected' => $selected,
'Disabled' => $disabled,
)); ));
} }
} }
@ -167,6 +186,24 @@ class DropdownField extends FormField {
return parent::Field($properties); return parent::Field($properties);
} }
/**
* Mark certain elements as disabled,
* regardless of the {@link setDisabled()} settings.
*
* @param array $items Collection of array keys, as defined in the $source array
*/
public function setDisabledItems($items){
$this->disabledItems = $items;
return $this;
}
/**
* @return Array
*/
public function getDisabledItems(){
return $this->disabledItems;
}
public function getAttributes() { public function getAttributes() {
return array_merge( return array_merge(

View File

@ -34,6 +34,22 @@
* ) * )
* </code> * </code>
* *
* <b>Disabling individual items</b>
*
* <code>
* $groupedDrDownField->setDisabledItems(
* array(
* "numbers" => array(
* "1" => "1",
* "3" => "3"
* ),
* "letters" => array(
* "3" => "C"
* )
* )
* )
* </code>
*
* @package forms * @package forms
* @subpackage fields-basic * @subpackage fields-basic
*/ */
@ -45,13 +61,23 @@ class GroupedDropdownField extends DropdownField {
if(is_array($title)) { if(is_array($title)) {
$options .= "<optgroup label=\"$value\">"; $options .= "<optgroup label=\"$value\">";
foreach($title as $value2 => $title2) { foreach($title as $value2 => $title2) {
$disabled = '';
if( array_key_exists($value, $this->disabledItems)
&& is_array($this->disabledItems[$value])
&& in_array($value2, $this->disabledItems[$value]) ){
$disabled = 'disabled="disabled"';
}
$selected = $value2 == $this->value ? " selected=\"selected\"" : ""; $selected = $value2 == $this->value ? " selected=\"selected\"" : "";
$options .= "<option$selected value=\"$value2\">$title2</option>"; $options .= "<option$selected value=\"$value2\" $disabled>$title2</option>";
} }
$options .= "</optgroup>"; $options .= "</optgroup>";
} else { // Fall back to the standard dropdown field } else { // Fall back to the standard dropdown field
$disabled = '';
if( in_array($value, $this->disabledItems) ){
$disabled = 'disabled="disabled"';
}
$selected = $value == $this->value ? " selected=\"selected\"" : ""; $selected = $value == $this->value ? " selected=\"selected\"" : "";
$options .= "<option$selected value=\"$value\">$title</option>"; $options .= "<option$selected value=\"$value\" $disabled>$title</option>";
} }
} }

View File

@ -83,6 +83,8 @@ class DropdownFieldTest extends SapphireTest {
public function testNumberOfSelectOptionsAvailable() { public function testNumberOfSelectOptionsAvailable() {
/* Create a field with a blank value */ /* Create a field with a blank value */
$field = $this->createDropdownField('(Any)'); $field = $this->createDropdownField('(Any)');
/* 3 options are available */
$this->assertEquals(count($this->findOptionElements($field->Field())), 3, '3 options are available'); $this->assertEquals(count($this->findOptionElements($field->Field())), 3, '3 options are available');
$selectedOptions = $this->findSelectedOptionElements($field->Field()); $selectedOptions = $this->findSelectedOptionElements($field->Field());
$this->assertEquals(count($selectedOptions), 1, $this->assertEquals(count($selectedOptions), 1,
@ -90,6 +92,8 @@ class DropdownFieldTest extends SapphireTest {
/* Create a field without a blank value */ /* Create a field without a blank value */
$field = $this->createDropdownField(); $field = $this->createDropdownField();
/* 2 options are available */
$this->assertEquals(count($this->findOptionElements($field->Field())), 2, '2 options are available'); $this->assertEquals(count($this->findOptionElements($field->Field())), 2, '2 options are available');
$selectedOptions = $this->findSelectedOptionElements($field->Field()); $selectedOptions = $this->findSelectedOptionElements($field->Field());
$this->assertEquals(count($selectedOptions), 0, 'There are no selected options'); $this->assertEquals(count($selectedOptions), 0, 'There are no selected options');
@ -127,6 +131,39 @@ class DropdownFieldTest extends SapphireTest {
'The selected option is "Cats and Kittens"'); 'The selected option is "Cats and Kittens"');
} }
public function testNumberOfDisabledOptions() {
/* Create a field with a blank value & set 0 & 1 to disabled */
$field = $this->createDropdownField('(Any)');
$field->setDisabledItems(array(0,1));
/* 3 options are available */
$this->assertEquals(count($this->findOptionElements($field->Field())), 3, '3 options are available');
/* There are 2 disabled options */
$disabledOptions = $this->findDisabledOptionElements($field->Field());
$this->assertEquals(count($disabledOptions), 2, 'We have 2 disabled options');
/* Create a field without a blank value & set 1 to disabled, then set none to disabled (unset) */
$field = $this->createDropdownField();
$field->setDisabledItems(array(1));
/* 2 options are available */
$this->assertEquals(count($this->findOptionElements($field->Field())), 2, '2 options are available');
/* get disabled items returns an array of one */
$this->assertEquals(
$field->getDisabledItems(),
array( 1 )
);
/* unset disabled items */
$field->setDisabledItems(array());
/* There are no disabled options anymore */
$disabledOptions = $this->findDisabledOptionElements($field->Field());
$this->assertEquals(count($disabledOptions), 0, 'There are no disabled options');
}
/** /**
* Create a test dropdown field, with the option to * Create a test dropdown field, with the option to
* set what source and blank value it should contain * set what source and blank value it should contain
@ -186,4 +223,29 @@ class DropdownFieldTest extends SapphireTest {
return $foundSelected; return $foundSelected;
} }
} /**
* Find all the <OPTION> elements from a
* string of HTML that have the "disabled"
* attribute.
*
* @param string $html HTML to parse for elements
* @return array of SimpleXMLElement objects
*/
public function findDisabledOptionElements($html) {
$options = $this->findOptionElements($html);
/* Find any elements that have the "disabled" attribute and put them into a list */
$foundDisabled = array();
foreach($options as $option) {
$attributes = $option->attributes();
if($attributes) foreach($attributes as $attribute => $value) {
if($attribute == 'disabled') {
$foundDisabled[] = $option;
}
}
}
return $foundDisabled;
}
}