Skip to content

Commit f088f45

Browse files
committed
SelectBox: only one item can be rendered as selected at the same time
1 parent 8b45275 commit f088f45

7 files changed

Lines changed: 49 additions & 5 deletions

File tree

examples/custom-control.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public function getControl()
9393

9494
. Helpers::createSelectBox(
9595
[1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
96-
['selected?' => $this->month]
96+
[],
97+
$this->month
9798
)->name($name . '[month]')
9899

99100
. Html::el('input', [

src/Forms/Controls/MultiSelectBox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public function getControl(): Nette\Utils\Html
6969
return Nette\Forms\Helpers::createSelectBox(
7070
$items,
7171
[
72-
'selected?' => $this->value,
7372
'disabled:' => is_array($this->disabled) ? $this->disabled : NULL,
74-
] + $this->optionAttributes
73+
] + $this->optionAttributes,
74+
$this->value
7575
)->addAttributes(parent::getControl()->attrs)->multiple(TRUE);
7676
}
7777

src/Forms/Controls/SelectBox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ public function getControl(): Nette\Utils\Html
9999
return Nette\Forms\Helpers::createSelectBox(
100100
$items,
101101
[
102-
'selected?' => $this->value,
103102
'disabled:' => is_array($this->disabled) ? $this->disabled : NULL,
104-
] + $this->optionAttributes
103+
] + $this->optionAttributes,
104+
$this->value
105105
)->addAttributes(parent::getControl()->attrs);
106106
}
107107

src/Forms/Helpers.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ public static function createSelectBox(array $items, array $optionAttrs = NULL,
191191
. htmlspecialchars((string) $caption, ENT_NOQUOTES, 'UTF-8')
192192
. '</option>';
193193
}
194+
if ($selected === $value) {
195+
unset($optionAttrs['selected'], $option->attrs['selected']);
196+
}
194197
}
195198
$res .= $tmp;
196199
$tmp = '';

tests/Forms/Controls.MultiSelectBox.render.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ test(function () { // selected
5050
});
5151

5252

53+
test(function () { // selected 2x
54+
$form = new Form;
55+
$input = $form->addMultiSelect('list', 'Label', [
56+
['a' => 'First'],
57+
['a' => 'First'],
58+
])->setValue('a');
59+
60+
Assert::same('<select name="list[]" id="frm-list" multiple><optgroup label="0"><option value="a" selected>First</option></optgroup><optgroup label="1"><option value="a" selected>First</option></optgroup></select>', (string) $input->getControl());
61+
});
62+
63+
5364
test(function () { // translator & groups
5465
$form = new Form;
5566
$input = $form->addMultiSelect('list', 'Label', [

tests/Forms/Controls.SelectBox.render.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ test(function () { // selected
5050
});
5151

5252

53+
test(function () { // selected 2x
54+
$form = new Form;
55+
$input = $form->addSelect('list', 'Label', [
56+
['a' => 'First'],
57+
['a' => 'First'],
58+
])->setValue('a');
59+
60+
Assert::same('<select name="list" id="frm-list"><optgroup label="0"><option value="a" selected>First</option></optgroup><optgroup label="1"><option value="a">First</option></optgroup></select>', (string) $input->getControl());
61+
});
62+
63+
5364
test(function () { // translator & groups
5465
$form = new Form;
5566
$input = $form->addSelect('list', 'Label', [

tests/Forms/Helpers.createSelectBox.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ test(function () {
8787
)
8888
);
8989

90+
Assert::same(
91+
'<select><optgroup label="0"><option value="a" selected>First</option></optgroup><optgroup label="1"><option value="a">First</option></optgroup></select>',
92+
(string) Helpers::createSelectBox(
93+
[['a' => 'First'], ['a' => 'First']],
94+
[],
95+
'a'
96+
)
97+
);
98+
9099
Assert::same(
91100
'<select><option value="a" selected>First</option><option value="b" selected>Second</option></select>',
92101
(string) Helpers::createSelectBox(
@@ -95,4 +104,13 @@ test(function () {
95104
['a', 'b']
96105
)
97106
);
107+
108+
Assert::same(
109+
'<select><optgroup label="0"><option value="a" selected>First</option></optgroup><optgroup label="1"><option value="a" selected>First</option></optgroup></select>',
110+
(string) Helpers::createSelectBox(
111+
[['a' => 'First'], ['a' => 'First']],
112+
[],
113+
['a', 'b']
114+
)
115+
);
98116
});

0 commit comments

Comments
 (0)