• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / forms / 21851792823

10 Feb 2026 04:31AM UTC coverage: 93.412% (-0.07%) from 93.481%
21851792823

push

github

dg
component/model 4 WIP

2070 of 2216 relevant lines covered (93.41%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.23
/src/Forms/Controls/SelectBox.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Forms\Controls;
11

12
use Nette;
13
use Stringable;
14
use function array_key_exists, is_array;
15

16

17
/**
18
 * Select box control that allows single item selection.
19
 */
20
class SelectBox extends ChoiceControl
21
{
22
        /** validation rule */
23
        public const Valid = ':selectBoxValid';
24

25
        /** @deprecated use SelectBox::Valid */
26
        public const VALID = self::Valid;
27

28
        /** @var mixed[]  option / optgroup */
29
        private array $options = [];
30
        private string|Stringable|false $prompt = false;
31

32
        /** @var array<string, mixed> */
33
        private array $optionAttributes = [];
34

35

36
        /** @param  ?mixed[]  $items */
37
        public function __construct(string|\Stringable|null $label = null, ?array $items = null)
1✔
38
        {
39
                parent::__construct($label, $items);
1✔
40
                $this->setOption('type', 'select');
1✔
41
                $this->addCondition(
1✔
42
                        fn() => $this->prompt === false
1✔
43
                        && $this->options
1✔
44
                        && $this->control->size < 2,
1✔
45
                )->addRule(Nette\Forms\Form::Filled, Nette\Forms\Validator::$messages[self::Valid]);
1✔
46
        }
1✔
47

48

49
        /**
50
         * Sets first prompt item in select box.
51
         */
52
        public function setPrompt(string|Stringable|false $prompt): static
1✔
53
        {
54
                $this->prompt = $prompt;
1✔
55
                return $this;
1✔
56
        }
57

58

59
        /**
60
         * Returns first prompt item.
61
         */
62
        public function getPrompt(): string|Stringable|false
63
        {
64
                return $this->prompt;
×
65
        }
66

67

68
        /**
69
         * Sets options and option groups from which to choose.
70
         * @param  mixed[]  $items
71
         * @return static
72
         */
73
        public function setItems(array $items, bool $useKeys = true)
1✔
74
        {
75
                if (!$useKeys) {
1✔
76
                        $res = [];
1✔
77
                        foreach ($items as $key => $value) {
1✔
78
                                unset($items[$key]);
1✔
79
                                if (is_array($value)) {
1✔
80
                                        foreach ($value as $val) {
1✔
81
                                                $res[$key][(string) $val] = $val;
1✔
82
                                        }
83
                                } else {
84
                                        $res[(string) $value] = $value;
1✔
85
                                }
86
                        }
87

88
                        $items = $res;
1✔
89
                }
90

91
                $this->options = $items;
1✔
92
                return parent::setItems(Nette\Utils\Arrays::flatten($items, preserveKeys: true));
1✔
93
        }
94

95

96
        public function getControl(): Nette\Utils\Html
97
        {
98
                $items = [];
1✔
99
                foreach ($this->options as $key => $value) {
1✔
100
                        $items[is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
1✔
101
                }
102

103
                $attrs = $this->optionAttributes;
1✔
104
                $attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : [];
1✔
105

106
                $selected = $this->value;
1✔
107
                if ($this->prompt !== false) {
1✔
108
                        $promptKey = '';
1✔
109
                        while (isset($items[$promptKey])) {
1✔
110
                                $promptKey .= "\t";
1✔
111
                        }
112
                        $items = [$promptKey => $this->translate($this->prompt)] + $items;
1✔
113
                        if ($this->isRequired()) {
1✔
114
                                $attrs['hidden:'][$promptKey] = $attrs['disabled:'][$promptKey] = true;
1✔
115
                                // disabled & selected for Safari, hidden for other browsers
116
                                $selected = $this->value !== null && array_key_exists($this->value, $this->getItems()) ? $this->value : $promptKey;
1✔
117
                        }
118
                }
119

120
                return Nette\Forms\Helpers::createSelectBox($items, $attrs, $selected)
1✔
121
                        ->addAttributes(parent::getControl()->attrs);
1✔
122
        }
123

124

125
        /**
126
         * @param  array<string, mixed>  $attributes
127
         * @deprecated use setOptionAttribute()
128
         */
129
        public function addOptionAttributes(array $attributes): static
1✔
130
        {
131
                $this->optionAttributes = $attributes + $this->optionAttributes;
1✔
132
                return $this;
1✔
133
        }
134

135

136
        public function setOptionAttribute(string $name, mixed $value = true): static
1✔
137
        {
138
                $this->optionAttributes[$name] = $value;
1✔
139
                return $this;
1✔
140
        }
141

142

143
        public function isOk(): bool
144
        {
145
                return $this->isDisabled()
1✔
146
                        || $this->prompt !== false
1✔
147
                        || $this->getValue() !== null
1✔
148
                        || !$this->options
1✔
149
                        || $this->control->size > 1;
1✔
150
        }
151

152

153
        /** @return array<string, mixed> */
154
        public function getOptionAttributes(): array
155
        {
156
                return $this->optionAttributes;
×
157
        }
158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc