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

nette / forms / 26452888331

26 May 2026 02:01PM UTC coverage: 93.307% (+0.07%) from 93.241%
26452888331

push

github

dg
added CLAUDE.md

2105 of 2256 relevant lines covered (93.31%)

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 declare(strict_types=1);
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
namespace Nette\Forms\Controls;
9

10
use Nette;
11
use Stringable;
12
use function array_key_exists, is_array;
13

14

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

23
        /** @deprecated use SelectBox::Valid */
24
        public const VALID = self::Valid;
25

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

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

33

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

46

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

56

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

65

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

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

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

93

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

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

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

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

122

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

133

134
        /**
135
         * Sets an attribute on all <option> elements. Use trailing ':' for per-item values or '?' for boolean attributes.
136
         */
137
        public function setOptionAttribute(string $name, mixed $value = true): static
1✔
138
        {
139
                $this->optionAttributes[$name] = $value;
1✔
140
                return $this;
1✔
141
        }
142

143

144
        /**
145
         * Checks whether the current selection is valid (a non-prompt item is selected, or the control has a prompt/is disabled).
146
         */
147
        public function isOk(): bool
148
        {
149
                return $this->isDisabled()
1✔
150
                        || $this->prompt !== false
1✔
151
                        || $this->getValue() !== null
1✔
152
                        || !$this->options
1✔
153
                        || $this->control->size > 1;
1✔
154
        }
155

156

157
        /**
158
         * Returns all option attributes.
159
         * @return array<string, mixed>
160
         */
161
        public function getOptionAttributes(): array
162
        {
163
                return $this->optionAttributes;
×
164
        }
165
}
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