diff --git a/src/Forms/Controls/SelectBox.php b/src/Forms/Controls/SelectBox.php index 16516ca9..7207c2c2 100644 --- a/src/Forms/Controls/SelectBox.php +++ b/src/Forms/Controls/SelectBox.php @@ -96,17 +96,22 @@ public function getControl(): Nette\Utils\Html } $attrs = $this->optionAttributes; - $attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : null; + $attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : []; + $selected = $this->value; if ($this->prompt !== false) { $promptKey = ''; while (isset($items[$promptKey])) { $promptKey .= "\x1"; } $items = [$promptKey => $this->translate($this->prompt)] + $items; + if ($this->isRequired()) { + $attrs['hidden:'][$promptKey] = $attrs['disabled:'][$promptKey] = true; + $selected ??= $promptKey; // disabled & selected for Safari, hidden for other browsers + } } - return Nette\Forms\Helpers::createSelectBox($items, $attrs, $this->value) + return Nette\Forms\Helpers::createSelectBox($items, $attrs, $selected) ->addAttributes(parent::getControl()->attrs); } diff --git a/tests/Forms/Controls.SelectBox.render.phpt b/tests/Forms/Controls.SelectBox.render.phpt index 7d2b722f..fdbf672d 100644 --- a/tests/Forms/Controls.SelectBox.render.phpt +++ b/tests/Forms/Controls.SelectBox.render.phpt @@ -61,6 +61,36 @@ test('selected 2x', function () { }); +test('prompt', function () { + $form = new Form; + $input = $form->addSelect('list', 'Label', [ + 'a' => 'First', + 0 => 'Second', + ])->setPrompt('prompt'); + + Assert::same('', (string) $input->getControl()); + + $input->setValue(0); + + Assert::same('', (string) $input->getControl()); +}); + + +test('prompt + required', function () { + $form = new Form; + $input = $form->addSelect('list', 'Label', [ + 'a' => 'First', + 0 => 'Second', + ])->setPrompt('prompt')->setRequired(); + + Assert::same('', (string) $input->getControl()); + + $input->setValue(0); + + Assert::same('', (string) $input->getControl()); +}); + + test('unique prompt', function () { $form = new Form; $input = $form->addSelect('list', 'Label', [