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

nette / forms / 20836962018

09 Jan 2026 12:35AM UTC coverage: 93.481%. Remained the same
20836962018

push

github

dg
Container::setValues() and setDefaults() accepts array|Traversable|stdClass (BC break)

3 of 4 new or added lines in 1 file covered. (75.0%)

44 existing lines in 9 files now uncovered.

2065 of 2209 relevant lines covered (93.48%)

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

47

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

57

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

66

67
        /**
68
         * Sets options and option groups from which to choose.
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
        /** @deprecated use setOptionAttribute() */
124
        public function addOptionAttributes(array $attributes): static
1✔
125
        {
126
                $this->optionAttributes = $attributes + $this->optionAttributes;
1✔
127
                return $this;
1✔
128
        }
129

130

131
        public function setOptionAttribute(string $name, mixed $value = true): static
1✔
132
        {
133
                $this->optionAttributes[$name] = $value;
1✔
134
                return $this;
1✔
135
        }
136

137

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

147

148
        /** @return array<string, mixed> */
149
        public function getOptionAttributes(): array
150
        {
UNCOV
151
                return $this->optionAttributes;
×
152
        }
153
}
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