Skip to content

Commit

Permalink
Add empty option support.
Browse files Browse the repository at this point in the history
This is a useful option that we should keep from 2.x
  • Loading branch information
markstory committed Jan 7, 2014
1 parent 569856d commit 013b62c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
13 changes: 10 additions & 3 deletions Cake/View/Input/SelectBox.php
Expand Up @@ -72,17 +72,24 @@ public function render($data) {

protected function _renderOptions($data) {
$out = [];
if (!isset($data['options'])) {
$data['options'] = [];
}
$options = $data['options'];

if (!empty($data['empty'])) {
// TODO
$value = $data['empty'] === true ? '' : $data['empty'];
$empty = ['' => $value];
$options = $empty + $options;
}
if (empty($data['options'])) {
if (empty($options)) {
return $out;
}

$selected = isset($data['value']) ? $data['value'] : null;
$selectedArray = is_array($selected);

foreach ($data['options'] as $key => $val) {
foreach ($options as $key => $val) {
$template = 'option';
$strict = !is_numeric($key);

Expand Down
76 changes: 74 additions & 2 deletions Test/TestCase/View/Input/SelectBoxTest.php
Expand Up @@ -87,7 +87,41 @@ public function testRenderSimple() {
* @return void
*/
public function testRenderSelected() {
$this->markTestIncomplete('Not done');
$context = new Context();
$select = new SelectBox($this->templates, $context);
$data = [
'id' => 'BirdName',
'name' => 'Birds[name]',
'value' => '1',
'options' => [
1 => 'one',
'1x' => 'one x',
'2' => 'two',
'2x' => 'two x',
]
];
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
['option' => ['value' => '1x']], 'one x', '/option',
['option' => ['value' => '2']], 'two', '/option',
['option' => ['value' => '2x']], 'two x', '/option',
'/select'
];
$this->assertTags($result, $expected);

$data['value'] = 2;
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
['option' => ['value' => '1']], 'one', '/option',
['option' => ['value' => '1x']], 'one x', '/option',
['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
['option' => ['value' => '2x']], 'two x', '/option',
'/select'
];
$this->assertTags($result, $expected);
}

/**
Expand Down Expand Up @@ -141,7 +175,45 @@ public function testRenderDisabled() {
* @return void
*/
public function testRenderEmptyOption() {
$this->markTestIncomplete('Not done');
$select = new SelectBox($this->templates, $context);
$data = [
'id' => 'BirdName',
'name' => 'Birds[name]',
'empty' => true,
'options' => ['a' => 'Albatross', 'b' => 'Budgie']
];
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
['option' => ['value' => '']], '/option',
['option' => ['value' => 'a']], 'Albatross', '/option',
['option' => ['value' => 'b']], 'Budgie', '/option',
'/select'
];
$this->assertTags($result, $expected);

$data['empty'] = 'empty';
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
['option' => ['value' => '']], 'empty', '/option',
['option' => ['value' => 'a']], 'Albatross', '/option',
['option' => ['value' => 'b']], 'Budgie', '/option',
'/select'
];
$this->assertTags($result, $expected);

$data['empty'] = 'empty';
$data['value'] = '';
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option',
['option' => ['value' => 'a']], 'Albatross', '/option',
['option' => ['value' => 'b']], 'Budgie', '/option',
'/select'
];
$this->assertTags($result, $expected);
}

}

0 comments on commit 013b62c

Please sign in to comment.