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

nette / forms / 8502286652

01 Apr 2024 01:05AM UTC coverage: 93.333% (+0.003%) from 93.33%
8502286652

push

github

dg
SelectBox: prompt <option> is hidden

- only makes sense for the required element
- workaround for Safari: disabled & selected attributes

6 of 6 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

2086 of 2235 relevant lines covered (93.33%)

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

15

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

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

27
        /** of option / optgroup */
28
        private array $options = [];
29
        private string|Stringable|false $prompt = false;
30
        private array $optionAttributes = [];
31

32

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

44

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

54

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

63

64
        /**
65
         * Sets options and option groups from which to choose.
66
         * @return static
67
         */
68
        public function setItems(array $items, bool $useKeys = true)
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:'] = is_array($this->disabled) ? $this->disabled : [];
1✔
100

101
                $selected = $this->value;
1✔
102
                if ($this->prompt !== false) {
1✔
103
                        $promptKey = '';
1✔
104
                        while (isset($items[$promptKey])) {
1✔
105
                                $promptKey .= "\x1";
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
                                $selected ??= $promptKey; // disabled & selected for Safari, hidden for other browsers
1✔
111
                        }
112
                }
113

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

118

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

126

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

133

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

143

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