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

nette / forms / 15763260878

19 Jun 2025 05:19PM UTC coverage: 93.011%. Remained the same
15763260878

push

github

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

2076 of 2232 relevant lines covered (93.01%)

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
        /** of option / optgroup */
29
        private array $options = [];
30
        private string|Stringable|false $prompt = false;
31
        private array $optionAttributes = [];
32

33

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

45

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

55

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

64

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

83
                        $items = $res;
1✔
84
                }
85

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

90

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

98
                $attrs = $this->optionAttributes;
1✔
99
                $attrs['disabled:'] = $this->disabledChoices;
1✔
100

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

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

119

120
        /** @deprecated use setOptionAttribute() */
121
        public function addOptionAttributes(array $attributes): static
1✔
122
        {
123
                $this->optionAttributes = $attributes + $this->optionAttributes;
1✔
124
                return $this;
1✔
125
        }
126

127

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

134

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

144

145
        public function getOptionAttributes(): array
146
        {
147
                return $this->optionAttributes;
×
148
        }
149
}
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

© 2025 Coveralls, Inc