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

nette / forms / 21851967935

10 Feb 2026 04:40AM UTC coverage: 93.076% (+0.2%) from 92.892%
21851967935

push

github

dg
netteForms: restructured package, includes UMD and ESM (BC break)

2070 of 2224 relevant lines covered (93.08%)

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
         */
72
        public function setItems(array $items, bool $useKeys = true): static
1✔
73
        {
74
                if (!$useKeys) {
1✔
75
                        $res = [];
1✔
76
                        foreach ($items as $key => $value) {
1✔
77
                                unset($items[$key]);
1✔
78
                                if (is_array($value)) {
1✔
79
                                        foreach ($value as $val) {
1✔
80
                                                $res[$key][(string) $val] = $val;
1✔
81
                                        }
82
                                } else {
83
                                        $res[(string) $value] = $value;
1✔
84
                                }
85
                        }
86

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

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

94

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

102
                $attrs = $this->optionAttributes;
1✔
103
                $attrs['disabled:'] = $this->disabledChoices;
1✔
104

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

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

123

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

134

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

141

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

151

152
        /** @return array<string, mixed> */
153
        public function getOptionAttributes(): array
154
        {
155
                return $this->optionAttributes;
×
156
        }
157
}
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