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

nette / forms / 8502288759

01 Apr 2024 01:05AM UTC coverage: 93.113% (+0.003%) from 93.11%
8502288759

push

github

dg
added HTML attribute data-nette-error

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

7 existing lines in 2 files now uncovered.

2082 of 2236 relevant lines covered (93.11%)

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

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

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

89

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

97
                $attrs = $this->optionAttributes;
1✔
98
                $attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : [];
1✔
99

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

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

117

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

125

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

132

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

142

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