Skip to content

Commit

Permalink
Handle optgroups with integer values better.
Browse files Browse the repository at this point in the history
Instead of failing on these keys because they are integers, also check
for the 'value' key being missing. This key missing is a pretty clear
indication that the option is not a complex option definition, and
likely an optgroup.

Refs #7361
  • Loading branch information
markstory committed Sep 8, 2015
1 parent 0049cf6 commit 3d6eb08
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/View/Widget/SelectBoxWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected function _renderOptions($options, $disabled, $selected, $escape)
// Option groups
$arrayVal = (is_array($val) || $val instanceof Traversable);
if ((!is_int($key) && $arrayVal) ||
(is_int($key) && $arrayVal && isset($val['options']))
(is_int($key) && $arrayVal && (isset($val['options']) || !isset($val['value'])))
) {
$out[] = $this->_renderOptgroup($key, $val, $disabled, $selected, $escape);
continue;
Expand All @@ -239,7 +239,7 @@ protected function _renderOptions($options, $disabled, $selected, $escape)
'value' => $key,
'text' => $val,
];
if (is_array($val) && isset($optAttrs['text'], $optAttrs['value'])) {
if (is_array($val) && isset($val['text'], $val['value'])) {
$optAttrs = $val;
$key = $optAttrs['value'];
}
Expand Down
42 changes: 42 additions & 0 deletions tests/TestCase/View/Widget/SelectBoxWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,48 @@ public function testRenderOptionGroups()
$this->assertHtml($expected, $result);
}

/**
* test rendering with numeric option group keys
*
* @return void
*/
public function testRenderOptionGroupsIntegerKeys()
{
$select = new SelectBoxWidget($this->templates);
$data = [
'name' => 'Year[key]',
'options' => [
2014 => [
'key' => 'value'
],
2013 => [
'text' => '2013-text',
'options' => [
'key2' => 'value2'
]
]
]
];
$result = $select->render($data, $this->context);
$expected = [
'select' => [
'name' => 'Year[key]',
],
['optgroup' => ['label' => '2014']],
['option' => ['value' => 'key']],
'value',
'/option',
'/optgroup',
['optgroup' => ['label' => '2013-text']],
['option' => ['value' => 'key2']],
'value2',
'/option',
'/optgroup',
'/select'
];
$this->assertHtml($expected, $result);
}

/**
* test rendering with option groups and escaping
*
Expand Down

0 comments on commit 3d6eb08

Please sign in to comment.