Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix support for Traversable when creating option groups.

  • Loading branch information...
commit a2d7f8d6b8094a5a034d88c8c6a3320e7615dbda 1 parent 739c4dd
@markstory markstory authored
View
3  Cake/View/Input/SelectBox.php
@@ -15,6 +15,7 @@
namespace Cake\View\Input;
use Cake\View\StringTemplate;
+use Traversable;
/**
* Input widget class for generating a selectbox.
@@ -129,7 +130,7 @@ protected function _renderContent($data) {
protected function _renderOptions($options, $disabled, $selected, $escape) {
$out = [];
foreach ($options as $key => $val) {
- if (is_array($val)) {
+ if (is_array($val) || $val instanceof Traversable) {
$groupOptions = $this->_renderOptions($val, $disabled, $selected, $escape);
$out[] = $this->_templates->format('optgroup', [
'label' => $escape ? h($key) : $key,
View
44 Test/TestCase/View/Input/SelectBoxTest.php
@@ -230,6 +230,50 @@ public function testRenderOptionGroups() {
}
/**
+ * test rendering with option groups with traversable nodes
+ *
+ * @return void
+ */
+ public function testRenderOptionGroupsTraversable() {
+ $select = new SelectBox($this->templates);
+ $mammals = new \ArrayObject(['beaver' => 'Beaver', 'elk' => 'Elk']);
+ $data = [
+ 'name' => 'Birds[name]',
+ 'options' => [
+ 'Mammal' => $mammals,
+ 'Bird' => [
+ 'budgie' => 'Budgie',
+ 'eagle' => 'Eagle',
+ ]
+ ]
+ ];
+ $result = $select->render($data);
+ $expected = [
+ 'select' => [
+ 'name' => 'Birds[name]',
+ ],
+ ['optgroup' => ['label' => 'Mammal']],
+ ['option' => ['value' => 'beaver']],
+ 'Beaver',
+ '/option',
+ ['option' => ['value' => 'elk']],
+ 'Elk',
+ '/option',
+ '/optgroup',
+ ['optgroup' => ['label' => 'Bird']],
+ ['option' => ['value' => 'budgie']],
+ 'Budgie',
+ '/option',
+ ['option' => ['value' => 'eagle']],
+ 'Eagle',
+ '/option',
+ '/optgroup',
+ '/select'
+ ];
+ $this->assertTags($result, $expected);
+ }
+
+/**
* test rendering option groups and selected values
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.