Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for rendering complex option types.

  • Loading branch information...
commit 78d7bbb44f7144330d726f547f771f9b0f20cfb1 1 parent de15f68
@markstory markstory authored
View
16 Cake/View/Input/SelectBox.php
@@ -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;
}
@@ -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']),
]);
}
}
View
30 Test/TestCase/View/Input/SelectBoxTest.php
@@ -77,6 +77,36 @@ public function testRenderSimple() {
}
/**
+ * 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
*
* @return void

5 comments on commit 78d7bbb

@renan
Collaborator

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
Owner

How would someone set attributes for an optgroup?

@renan
Collaborator

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
Owner

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

@markstory
Owner

Good idea. Done in 8e71c0a

Please sign in to comment.
Something went wrong with that request. Please try again.