Skip to content

Commit d4dc7e8

Browse files
bartko-sdg
authored andcommitted
Fix selectBox
1 parent 7858431 commit d4dc7e8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Forms/Controls/SelectBox.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function getControl(): Nette\Utils\Html
105105
}
106106
$items = [$promptKey => $this->translate($this->prompt)] + $items;
107107
if ($this->isRequired()) {
108+
$selected = array_key_exists($this->value, $this->getItems()) ? $this->value : null;
108109
$attrs['hidden:'][$promptKey] = $attrs['disabled:'][$promptKey] = true;
109110
$selected ??= $promptKey; // disabled & selected for Safari, hidden for other browsers
110111
}

tests/Forms/Controls.SelectBox.render.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ test('prompt + required', function () {
9090
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());
9191
});
9292

93+
test('prompt + checkDefaultValueToFalse + value does not exist', function () {
94+
$form = new Form;
95+
$input = $form->addSelect('list', 'Label', [
96+
'a' => 'First',
97+
0 => 'Second',
98+
])->setPrompt('prompt')->checkDefaultValue(false);
99+
100+
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());
101+
102+
$input->setValue('does not exists');
103+
104+
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());
105+
});
106+
107+
test('prompt + required + checkDefaultValueToFalse + value does not exist', function () {
108+
$form = new Form;
109+
$input = $form->addSelect('list', 'Label', [
110+
'a' => 'First',
111+
0 => 'Second',
112+
])->setPrompt('prompt')->setRequired()->checkDefaultValue(false);
113+
114+
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());
115+
116+
$input->setValue('does not exists');
117+
118+
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());
119+
});
93120

94121
test('unique prompt', function () {
95122
$form = new Form;

0 commit comments

Comments
 (0)