Skip to content

Commit

Permalink
Add disabled option support.
Browse files Browse the repository at this point in the history
Add disabled options and tests for them.
  • Loading branch information
markstory committed Jan 7, 2014
1 parent 5e25ee3 commit b0e4d7c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
23 changes: 21 additions & 2 deletions Cake/View/Input/SelectBox.php
Expand Up @@ -59,6 +59,10 @@ public function render($data) {
$options = $this->_renderOptions($data);
$name = $data['name'];
unset($data['name'], $data['options'], $data['empty'], $data['value']);
if (isset($data['disabled']) && is_array($data['disabled'])) {
unset($data['disabled']);
}

$attrs = $this->_parseAttributes($data);
return $this->_templates->format('select', [
'name' => $name,
Expand All @@ -84,13 +88,20 @@ protected function _renderOptions($data) {
}

$selected = isset($data['value']) ? $data['value'] : null;
$selectedArray = is_array($selected);
$disabled = null;
if (isset($data['disabled']) && is_array($data['disabled'])) {
$disabled = $data['disabled'];
}

foreach ($options as $key => $val) {
$template = 'option';
$isSelected = $this->_isSelected($key, $selected);
$isDisabled = $this->_isDisabled($key, $disabled);
if ($isSelected) {
$template = 'optionSelected';
$template .= 'Selected';
}
if ($isDisabled) {
$template .= 'Disabled';
}

$out[] = $this->_templates->format($template, [
Expand All @@ -113,6 +124,14 @@ protected function _isSelected($key, $selected) {
return in_array((string)$key, $selected, $strict);
}

protected function _isDisabled($key, $disabled) {
if ($disabled === null) {
return false;
}
$strict = !is_numeric($key);
return in_array((string)$key, $disabled, $strict);
}

protected function _parseAttributes($options, $exclude = null) {
$insertBefore = ' ';
$options = (array)$options + array('escape' => true);
Expand Down
45 changes: 42 additions & 3 deletions Test/TestCase/View/Input/SelectBoxTest.php
Expand Up @@ -29,6 +29,8 @@ public function setUp() {
'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
'option' => '<option value="{{name}}">{{value}}</option>',
'optionSelected' => '<option value="{{name}}" selected="selected">{{value}}</option>',
'optionDisabled' => '<option value="{{name}}" disabled="disabled">{{value}}</option>',
'optionSelectedDisabled' => '<option value="{{name}}" selected="selected" disabled="disabled">{{value}}</option>',
'optgroup' => '<optgroup label="{{label}}">{{content}}</optgroup>',
];
$this->templates = new StringTemplate();
Expand Down Expand Up @@ -209,15 +211,16 @@ public function testRenderDisabled() {
$data = [
'disabled' => true,
'name' => 'Birds[name]',
'options' => ['a' => 'Albatross', 'b' => 'Budgie']
'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
'value' => 'a',
];
$result = $select->render($data);
$expected = [
'select' => [
'name' => 'Birds[name]',
'disabled' => 'disabled',
],
['option' => ['value' => 'a']], 'Albatross', '/option',
['option' => ['value' => 'a', 'selected' => 'selected']], 'Albatross', '/option',
['option' => ['value' => 'b']], 'Budgie', '/option',
'/select'
];
Expand All @@ -230,7 +233,34 @@ public function testRenderDisabled() {
* @return void
*/
public function testRenderDisabledMultiple() {
$this->markTestIncomplete('Not done');
$select = new SelectBox($this->templates);
$data = [
'disabled' => ['a', 'c'],
'value' => 'a',
'name' => 'Birds[name]',
'options' => [
'a' => 'Albatross',
'b' => 'Budgie',
'c' => 'Canary',
]
];
$result = $select->render($data);
$expected = [
'select' => [
'name' => 'Birds[name]',
],
['option' => ['value' => 'a', 'selected' => 'selected', 'disabled' => 'disabled']],
'Albatross',
'/option',
['option' => ['value' => 'b']],
'Budgie',
'/option',
['option' => ['value' => 'c', 'disabled' => 'disabled']],
'Canary',
'/option',
'/select'
];
$this->assertTags($result, $expected);
}

/**
Expand Down Expand Up @@ -284,4 +314,13 @@ public function testRenderEmptyOption() {
$this->assertTags($result, $expected);
}

/**
* Test rendering with disabling escaping.
*
* @return void
*/
public function testRenderEscapingOption() {
$this->markTestIncomplete('Not done');
}

}

0 comments on commit b0e4d7c

Please sign in to comment.