Permalink
Browse files

NEW Disable items in DropdownField and GroupedDropdownField

  • Loading branch information...
1 parent f3c0669 commit 7f605cfb54e1f860a182d3001e0612fee7fd7970 @micschk micschk committed with chillu Oct 17, 2012
Showing with 128 additions and 3 deletions.
  1. +37 −0 forms/DropdownField.php
  2. +28 −2 forms/GroupedDropdownField.php
  3. +63 −1 tests/forms/DropdownFieldTest.php
View
@@ -66,6 +66,14 @@
* );
* </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 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
@@ -104,6 +112,11 @@ class DropdownField extends FormField {
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.
* @param $name The field name
* @param $title The field title
@@ -154,11 +167,17 @@ public function Field($properties = array()) {
$selected = ($value) ? $value == $this->value : $value === $this->value;
$this->isSelected = $selected;
}
+
+ $disabled = false;
+ if(in_array($value, $this->disabledItems) && $title != $this->emptyString ){
+ $disabled = 'disabled';
+ }
$options[] = new ArrayData(array(
'Title' => $title,
'Value' => $value,
'Selected' => $selected,
+ 'Disabled' => $disabled,
));
}
}
@@ -167,6 +186,24 @@ public function Field($properties = array()) {
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() {
return array_merge(
@@ -34,6 +34,22 @@
* )
* </code>
*
+ * <b>Disabling individual items</b>
+ *
+ * <code>
+ * $groupedDrDownField->setDisabledItems(
+ * array(
+ * "numbers" => array(
+ * "1" => "1",
+ * "3" => "3"
+ * ),
+ * "letters" => array(
+ * "3" => "C"
+ * )
+ * )
+ * )
+ * </code>
+ *
* @package forms
* @subpackage fields-basic
*/
@@ -45,13 +61,23 @@ public function Field($properties = array()) {
if(is_array($title)) {
$options .= "<optgroup label=\"$value\">";
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\"" : "";
- $options .= "<option$selected value=\"$value2\">$title2</option>";
+ $options .= "<option$selected value=\"$value2\" $disabled>$title2</option>";
}
$options .= "</optgroup>";
} else { // Fall back to the standard dropdown field
+ $disabled = '';
+ if( in_array($value, $this->disabledItems) ){
+ $disabled = 'disabled="disabled"';
+ }
$selected = $value == $this->value ? " selected=\"selected\"" : "";
- $options .= "<option$selected value=\"$value\">$title</option>";
+ $options .= "<option$selected value=\"$value\" $disabled>$title</option>";
}
}
@@ -83,13 +83,17 @@ public function testZeroArraySourceNotOverwrittenByEmptyString() {
public function testNumberOfSelectOptionsAvailable() {
/* Create a field with a blank value */
$field = $this->createDropdownField('(Any)');
+
+ /* 3 options are available */
$this->assertEquals(count($this->findOptionElements($field->Field())), 3, '3 options are available');
$selectedOptions = $this->findSelectedOptionElements($field->Field());
$this->assertEquals(count($selectedOptions), 1,
'We only have 1 selected option, since a dropdown can only possibly have one!');
/* Create a field without a blank value */
$field = $this->createDropdownField();
+
+ /* 2 options are available */
$this->assertEquals(count($this->findOptionElements($field->Field())), 2, '2 options are available');
$selectedOptions = $this->findSelectedOptionElements($field->Field());
$this->assertEquals(count($selectedOptions), 0, 'There are no selected options');
@@ -127,6 +131,39 @@ public function testStringValueSelectedOptionBehaviour() {
'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
* set what source and blank value it should contain
@@ -186,4 +223,29 @@ public function findSelectedOptionElements($html) {
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;
+ }
+
+}

0 comments on commit 7f605cf

Please sign in to comment.