Permalink
Browse files

Add empty option support.

This is a useful option that we should keep from 2.x
  • Loading branch information...
1 parent 569856d commit 013b62c38e127ce7eb16674469d6ee34759270e1 @markstory markstory committed Jan 7, 2014
Showing with 84 additions and 5 deletions.
  1. +10 −3 Cake/View/Input/SelectBox.php
  2. +74 −2 Test/TestCase/View/Input/SelectBoxTest.php
@@ -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);
@@ -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);
}
/**
@@ -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.