Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENHANCEMENT Added ListboxField->setDisabledItems() and setDefaultItem…

…s() (similar to CheckboxSetField API)
  • Loading branch information...
commit 5d565dcfa3e8ba71ffd19e12b00d2d88dff9196b 1 parent ea31e17
@chillu chillu authored
View
70 forms/ListboxField.php
@@ -40,6 +40,16 @@ class ListboxField extends DropdownField {
* @var boolean
*/
protected $multiple = false;
+
+ /**
+ * @var Array
+ */
+ protected $disabledItems = array();
+
+ /**
+ * @var Array
+ */
+ protected $defaultItems = array();
/**
* Creates a new dropdown field.
@@ -61,43 +71,31 @@ function __construct($name, $title = '', $source = array(), $value = '', $size =
* Returns a <select> tag containing all the appropriate <option> tags
*/
function Field($properties = array()) {
- if($this->multiple) {
- $this->name .= '[]';
- }
-
+ if($this->multiple) $this->name .= '[]';
$options = array();
// We have an array of values
if(is_array($this->value)){
// Loop through and figure out which values were selected.
-
foreach($this->getSource() as $value => $title) {
- // Loop through the array of values to find out if this value is selected.
- $selected = "";
- foreach($this->value as $v){
- if($value == $v) {
- $selected = " selected=\"selected\"";
- break;
- }
- }
$options[] = new ArrayData(array(
'Title' => $title,
'Value' => $value,
- 'Selected' => $selected,
+ 'Selected' => (in_array($value, $this->value) || in_array($value, $this->defaultItems)),
+ 'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
));
}
} else {
// Listbox was based a singlular value, so treat it like a dropdown.
foreach($this->getSource() as $value => $title) {
- $selected = $value == $this->value ? " selected=\"selected\"" : "";
$options[] = new ArrayData(array(
'Title' => $title,
'Value' => $value,
- 'Selected' => $selected,
+ 'Selected' => ($value == $this->value || in_array($value, $this->defaultItems)),
+ 'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
));
}
}
-
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
return $this->customise($properties)->renderWith($this->getTemplate());
}
@@ -214,7 +212,6 @@ function setValue($val, $obj = null) {
// Doesn't check against unknown values in order to allow for less rigid data handling.
// They're silently ignored and overwritten the next time the field is saved.
-
parent::setValue($parts);
} else {
if(!in_array($val, array_keys($this->source))) {
@@ -232,5 +229,42 @@ function setValue($val, $obj = null) {
return $this;
}
+
+ /**
+ * 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 $this;
+ }
+
+ /**
+ * @return Array
+ */
+ function getDisabledItems() {
+ return $this->disabledItems;
+ }
+
+ /**
+ * 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 $this;
+ }
+
+ /**
+ * @return Array
+ */
+ function getDefaultItems() {
+ return $this->defaultItems;
+ }
}
View
2  templates/forms/DropdownField.ss
@@ -1,5 +1,5 @@
<select $AttributesHTML>
<% control Options %>
- <option value="$Value"<% if Selected %> selected<% end_if %>>$Title</option>
+ <option value="$Value"<% if Selected %> selected="selected"<% end_if %><% if Disabled %> disabled="disabled"<% end_if %>>$Title</option>
<% end_control %>
</select>
View
22 tests/forms/ListboxFieldTest.php
@@ -27,6 +27,28 @@ function testFieldWithManyManyRelationship() {
$this->assertEquals('selected', (string)$tag2xml[0]['selected']);
$this->assertNull($tag3xml[0]['selected']);
}
+
+ function testFieldWithDisabledItems() {
+ $articleWithTags = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithtags');
+ $tag1 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag1');
+ $tag2 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag2');
+ $tag3 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag3');
+ $field = new ListboxField("Tags", "Test field", DataObject::get("ListboxFieldTest_Tag")->map()->toArray());
+ $field->setMultiple(true);
+ $field->setValue(null, $articleWithTags);
+ $field->setDisabledItems(array($tag1->ID, $tag3->ID));
+
+ $p = new CSSContentParser($field->Field());
+ $tag1xml = $p->getByXpath('//option[@value=' . $tag1->ID . ']');
+ $tag2xml = $p->getByXpath('//option[@value=' . $tag2->ID . ']');
+ $tag3xml = $p->getByXpath('//option[@value=' . $tag3->ID . ']');
+ $this->assertEquals('selected', (string)$tag1xml[0]['selected']);
+ $this->assertEquals('disabled', (string)$tag1xml[0]['disabled']);
+ $this->assertEquals('selected', (string)$tag2xml[0]['selected']);
+ $this->assertNull($tag2xml[0]['disabled']);
+ $this->assertNull($tag3xml[0]['selected']);
+ $this->assertEquals('disabled', (string)$tag3xml[0]['disabled']);
+ }
function testSaveIntoNullValueWithMultipleOff() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
Please sign in to comment.
Something went wrong with that request. Please try again.