Skip to content

Commit

Permalink
SelectBox: prompt key is always unique
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 1, 2024
1 parent 066a21b commit 2edbd6c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Forms/Controls/SelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,22 @@ public function setItems(array $items, bool $useKeys = true)

public function getControl(): Nette\Utils\Html
{
$items = $this->prompt === false ? [] : ['' => $this->translate($this->prompt)];
$items = [];
foreach ($this->options as $key => $value) {
$items[is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
}

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

if ($this->prompt !== false) {
$promptKey = '';
while (isset($items[$promptKey])) {
$promptKey .= "\x1";
}
$items = [$promptKey => $this->translate($this->prompt)] + $items;
}

return Nette\Forms\Helpers::createSelectBox($items, $attrs, $this->value)
->addAttributes(parent::getControl()->attrs);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Forms/Controls.SelectBox.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ test('selected 2x', function () {
});


test('unique prompt', function () {
$form = new Form;
$input = $form->addSelect('list', 'Label', [
'' => 'First',
])->setPrompt('prompt');

Assert::same('<select name="list" id="frm-list"><option value="' . "\x01" . '">prompt</option><option value="">First</option></select>', (string) $input->getControl());

$input->setValue('');

Assert::same('<select name="list" id="frm-list"><option value="' . "\x01" . '">prompt</option><option value="" selected>First</option></select>', (string) $input->getControl());
});


test('translator & groups', function () {
$form = new Form;
$input = $form->addSelect('list', 'Label', [
Expand Down

0 comments on commit 2edbd6c

Please sign in to comment.