Skip to content
Permalink
Browse files

Fix support for Traversable when creating option groups.

  • Loading branch information...
markstory committed Jan 8, 2014
1 parent 739c4dd commit a2d7f8d6b8094a5a034d88c8c6a3320e7615dbda
Showing with 46 additions and 1 deletion.
  1. +2 −1 Cake/View/Input/SelectBox.php
  2. +44 −0 Test/TestCase/View/Input/SelectBoxTest.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,
@@ -229,6 +229,50 @@ public function testRenderOptionGroups() {
$this->assertTags($result, $expected);
}
/**
* 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
*

0 comments on commit a2d7f8d

Please sign in to comment.
You can’t perform that action at this time.