Skip to content

Commit a948207

Browse files
committed
Helpers::createSelectBox: added parameter $selected
1 parent 6f8c9d1 commit a948207

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Forms/Helpers.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,11 @@ public static function createInputList(array $items, array $inputAttrs = NULL, a
165165
/**
166166
* @return Html
167167
*/
168-
public static function createSelectBox(array $items, array $optionAttrs = NULL)
168+
public static function createSelectBox(array $items, array $optionAttrs = NULL, $selected = NULL)
169169
{
170+
if ($selected !== NULL) {
171+
$optionAttrs['selected?'] = $selected;
172+
}
170173
list($optionAttrs, $optionTag) = self::prepareAttrs($optionAttrs, 'option');
171174
$option = Html::el();
172175
$res = $tmp = '';

tests/Forms/Helpers.createSelectBox.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,22 @@ test(function () {
7575
['disabled:' => TRUE, 'selected?' => 'b']
7676
)
7777
);
78+
79+
Assert::same(
80+
'<select><option value="a">First</option><option value="b" selected>Second</option></select>',
81+
(string) Helpers::createSelectBox(
82+
['a' => 'First', 'b' => 'Second'],
83+
[],
84+
'b'
85+
)
86+
);
87+
88+
Assert::same(
89+
'<select><option value="a" selected>First</option><option value="b" selected>Second</option></select>',
90+
(string) Helpers::createSelectBox(
91+
['a' => 'First', 'b' => 'Second'],
92+
[],
93+
['a', 'b']
94+
)
95+
);
7896
});

0 commit comments

Comments
 (0)