Skip to content

Commit a20fdb1

Browse files
committed
SelectBox: prompt <option> is hidden
- only makes sense for the required element - workaround for Safari: disabled & selected attributes
1 parent 2edbd6c commit a20fdb1

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Forms/Controls/SelectBox.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,22 @@ public function getControl(): Nette\Utils\Html
9696
}
9797

9898
$attrs = $this->optionAttributes;
99-
$attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : null;
99+
$attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : [];
100100

101+
$selected = $this->value;
101102
if ($this->prompt !== false) {
102103
$promptKey = '';
103104
while (isset($items[$promptKey])) {
104105
$promptKey .= "\x1";
105106
}
106107
$items = [$promptKey => $this->translate($this->prompt)] + $items;
108+
if ($this->isRequired()) {
109+
$attrs['hidden:'][$promptKey] = $attrs['disabled:'][$promptKey] = true;
110+
$selected ??= $promptKey; // disabled & selected for Safari, hidden for other browsers
111+
}
107112
}
108113

109-
return Nette\Forms\Helpers::createSelectBox($items, $attrs, $this->value)
114+
return Nette\Forms\Helpers::createSelectBox($items, $attrs, $selected)
110115
->addAttributes(parent::getControl()->attrs);
111116
}
112117

tests/Forms/Controls.SelectBox.render.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,36 @@ test('selected 2x', function () {
6161
});
6262

6363

64+
test('prompt', function () {
65+
$form = new Form;
66+
$input = $form->addSelect('list', 'Label', [
67+
'a' => 'First',
68+
0 => 'Second',
69+
])->setPrompt('prompt');
70+
71+
Assert::same('<select name="list" id="frm-list"><option value="">prompt</option><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());
72+
73+
$input->setValue(0);
74+
75+
Assert::same('<select name="list" id="frm-list"><option value="">prompt</option><option value="a">First</option><option value="0" selected>Second</option></select>', (string) $input->getControl());
76+
});
77+
78+
79+
test('prompt + required', function () {
80+
$form = new Form;
81+
$input = $form->addSelect('list', 'Label', [
82+
'a' => 'First',
83+
0 => 'Second',
84+
])->setPrompt('prompt')->setRequired();
85+
86+
Assert::same('<select name="list" id="frm-list" required data-nette-rules=\'[{"op":":filled","msg":"This field is required."}]\'><option value="" disabled hidden selected>prompt</option><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());
87+
88+
$input->setValue(0);
89+
90+
Assert::same('<select name="list" id="frm-list" required data-nette-rules=\'[{"op":":filled","msg":"This field is required."}]\'><option value="" disabled hidden>prompt</option><option value="a">First</option><option value="0" selected>Second</option></select>', (string) $input->getControl());
91+
});
92+
93+
6494
test('unique prompt', function () {
6595
$form = new Form;
6696
$input = $form->addSelect('list', 'Label', [

0 commit comments

Comments
 (0)