Skip to content

Commit

Permalink
Add support for rendering complex option types.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 8, 2014
1 parent de15f68 commit 78d7bbb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Cake/View/Input/SelectBox.php
Expand Up @@ -130,14 +130,20 @@ protected function _renderContent($data) {
protected function _renderOptions($options, $disabled, $selected, $escape) {
$out = [];
foreach ($options as $key => $val) {
if (is_array($val) || $val instanceof Traversable) {
if (!is_int($key) && is_array($val) || $val instanceof Traversable) {
$groupOptions = $this->_renderOptions($val, $disabled, $selected, $escape);
$out[] = $this->_templates->format('optgroup', [
'label' => $escape ? h($key) : $key,
'content' => implode('', $groupOptions)
]);
} else {
$optAttrs = [];
$optAttrs = [
'name' => $key,
'value' => $val,
];
if (is_array($val) && isset($optAttrs['name'], $optAttrs['value'])) {
$optAttrs = $val;
}
if ($this->_isSelected($key, $selected)) {
$optAttrs['selected'] = true;
}
Expand All @@ -147,9 +153,9 @@ protected function _renderOptions($options, $disabled, $selected, $escape) {
$optAttrs['escape'] = $escape;

$out[] = $this->_templates->format('option', [
'name' => $escape ? h($key) : $key,
'value' => $escape ? h($val) : $val,
'attrs' => $this->_templates->formatAttributes($optAttrs),
'name' => $escape ? h($optAttrs['name']) : $optAttrs['name'],
'value' => $escape ? h($optAttrs['value']) : $optAttrs['value'],
'attrs' => $this->_templates->formatAttributes($optAttrs, ['name', 'value']),
]);
}
}
Expand Down
30 changes: 30 additions & 0 deletions Test/TestCase/View/Input/SelectBoxTest.php
Expand Up @@ -76,6 +76,36 @@ public function testRenderSimple() {
$this->assertTags($result, $expected);
}

/**
* test complex option rendering
*
* @return void
*/
public function testRenderComplex() {
$select = new SelectBox($this->templates);
$data = [
'id' => 'BirdName',
'name' => 'Birds[name]',
'options' => [
['name' => 'a', 'value' => 'Albatross'],
['name' => 'b', 'value' => 'Budgie', 'data-foo' => 'bar'],
]
];
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
['option' => ['value' => 'a']],
'Albatross',
'/option',
['option' => ['value' => 'b', 'data-foo' => 'bar']],
'Budgie',
'/option',
'/select'
];
$this->assertTags($result, $expected);
}


/**
* test rendering with a selected value
*
Expand Down

5 comments on commit 78d7bbb

@renan
Copy link
Contributor

@renan renan commented on 78d7bbb Jan 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice going!

What do you think about supporting attributes on optgroup?
Hmm ... only disabled and data-* could be used though.
Not sure if really needed but that would offer complete control on what is being generated.

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would someone set attributes for an optgroup?

@renan
Copy link
Contributor

@renan renan commented on 78d7bbb Jan 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe with an options key?

eg:

$options = [
    [
        'name' => 'CakePHP 2.x',
        'data-foo' => 'bar',
        'options' => [
            '2.0' => 'CakePHP 2.0',
            '2.1' => 'CakePHP 2.1',
        ]
    ],
    '3.0.0-dev' => 'CakePHP 3.0.0-dev',
];

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could work. I'll give it a try.

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done in 8e71c0a

Please sign in to comment.