Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 462be3e36682128c7530e6069c486e9bce138d05 1 parent 9a8f6db
@markstory markstory authored
View
4 Cake/View/Input/SelectBox.php
@@ -99,6 +99,10 @@ protected function _renderContent($data) {
$out = [];
$options = $data['options'];
+ if ($options instanceof Traversable) {
+ $options = iterator_to_array($options);
+ }
+
if (!empty($data['empty'])) {
$value = $data['empty'] === true ? '' : $data['empty'];
$options = ['' => $value] + $options;
View
24 Test/TestCase/View/Input/SelectBoxTest.php
@@ -77,6 +77,30 @@ public function testRenderSimple() {
}
/**
+ * test simple iterator rendering
+ *
+ * @return void
+ */
+ public function testRenderSimpleIterator() {
+ $select = new SelectBox($this->templates);
+ $options = new \ArrayObject(['a' => 'Albatross', 'b' => 'Budgie']);
+ $data = [
+ 'name' => 'Birds[name]',
+ 'options' => $options,
+ 'empty' => true
+ ];
+ $result = $select->render($data);
+ $expected = [
+ 'select' => ['name' => 'Birds[name]'],
+ ['option' => ['value' => '']], '/option',
+ ['option' => ['value' => 'a']], 'Albatross', '/option',
+ ['option' => ['value' => 'b']], 'Budgie', '/option',
+ '/select'
+ ];
+ $this->assertTags($result, $expected);
+ }
+
+/**
* test complex option rendering
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.