Skip to content
Permalink
Browse files

Add disabled option support.

Add disabled options and tests for them.
  • Loading branch information...
markstory committed Jan 7, 2014
1 parent 5e25ee3 commit b0e4d7c84c79c68ea7199749c1db3972a2c11869
Showing with 63 additions and 5 deletions.
  1. +21 −2 Cake/View/Input/SelectBox.php
  2. +42 −3 Test/TestCase/View/Input/SelectBoxTest.php
@@ -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,
@@ -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, [
@@ -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);
@@ -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();
@@ -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'
];
@@ -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);
}
/**
@@ -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.
You can’t perform that action at this time.