Skip to content

Commit 462be3e

Browse files
committed
Convert iterators into arrays before mucking about with them.
$data['options'] might be an iterator. Convert it to an array before adding empty options or converting to option elements.
1 parent 9a8f6db commit 462be3e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Cake/View/Input/SelectBox.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ protected function _renderContent($data) {
9999
$out = [];
100100
$options = $data['options'];
101101

102+
if ($options instanceof Traversable) {
103+
$options = iterator_to_array($options);
104+
}
105+
102106
if (!empty($data['empty'])) {
103107
$value = $data['empty'] === true ? '' : $data['empty'];
104108
$options = ['' => $value] + $options;

Test/TestCase/View/Input/SelectBoxTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ public function testRenderSimple() {
7676
$this->assertTags($result, $expected);
7777
}
7878

79+
/**
80+
* test simple iterator rendering
81+
*
82+
* @return void
83+
*/
84+
public function testRenderSimpleIterator() {
85+
$select = new SelectBox($this->templates);
86+
$options = new \ArrayObject(['a' => 'Albatross', 'b' => 'Budgie']);
87+
$data = [
88+
'name' => 'Birds[name]',
89+
'options' => $options,
90+
'empty' => true
91+
];
92+
$result = $select->render($data);
93+
$expected = [
94+
'select' => ['name' => 'Birds[name]'],
95+
['option' => ['value' => '']], '/option',
96+
['option' => ['value' => 'a']], 'Albatross', '/option',
97+
['option' => ['value' => 'b']], 'Budgie', '/option',
98+
'/select'
99+
];
100+
$this->assertTags($result, $expected);
101+
}
102+
79103
/**
80104
* test complex option rendering
81105
*

0 commit comments

Comments
 (0)