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

nette / forms / 21852757615

10 Feb 2026 05:19AM UTC coverage: 92.701% (-0.4%) from 93.114%
21852757615

push

github

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

2032 of 2192 relevant lines covered (92.7%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/Forms/Controls/MultiSelectBox.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 function is_array;
14

15

16
/**
17
 * Select box control that allows multiple items selection.
18
 */
19
class MultiSelectBox extends MultiChoiceControl
20
{
21
        /** @var mixed[]  option / optgroup */
22
        private array $options = [];
23

24
        /** @var array<string, mixed> */
25
        private array $optionAttributes = [];
26

27

28
        /** @param  ?mixed[]  $items */
29
        public function __construct(string|\Stringable|null $label = null, ?array $items = null)
1✔
30
        {
31
                parent::__construct($label, $items);
1✔
32
                $this->setOption('type', 'select');
1✔
33
        }
1✔
34

35

36
        /**
37
         * Sets options and option groups from which to choose.
38
         * @param  mixed[]  $items
39
         */
40
        public function setItems(array $items, bool $useKeys = true): static
1✔
41
        {
42
                if (!$useKeys) {
1✔
43
                        $res = [];
1✔
44
                        foreach ($items as $key => $value) {
1✔
45
                                unset($items[$key]);
1✔
46
                                if (is_array($value)) {
1✔
47
                                        foreach ($value as $val) {
1✔
48
                                                $res[$key][(string) $val] = $val;
1✔
49
                                        }
50
                                } else {
51
                                        $res[(string) $value] = $value;
1✔
52
                                }
53
                        }
54

55
                        $items = $res;
1✔
56
                }
57

58
                $this->options = $items;
1✔
59
                return parent::setItems(Nette\Utils\Arrays::flatten($items, preserveKeys: true));
1✔
60
        }
61

62

63
        public function getControl(): Nette\Utils\Html
64
        {
65
                $items = [];
1✔
66
                foreach ($this->options as $key => $value) {
1✔
67
                        $items[is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
1✔
68
                }
69

70
                return Nette\Forms\Helpers::createSelectBox(
1✔
71
                        $items,
1✔
72
                        [
73
                                'disabled:' => $this->disabledChoices,
1✔
74
                        ] + $this->optionAttributes,
1✔
75
                        $this->value,
1✔
76
                )->addAttributes(parent::getControl()->attrs)->multiple(true);
1✔
77
        }
78

79

80
        /**
81
         * @param  array<string, mixed>  $attributes
82
         */
83
        #[\Deprecated('use setOptionAttribute()')]
84
        public function addOptionAttributes(array $attributes): static
85
        {
86
                trigger_error(__METHOD__ . '() is deprecated, use setOptionAttribute()', E_USER_DEPRECATED);
×
87
                $this->optionAttributes = $attributes + $this->optionAttributes;
×
88
                return $this;
×
89
        }
90

91

92
        public function setOptionAttribute(string $name, mixed $value = true): static
1✔
93
        {
94
                $this->optionAttributes[$name] = $value;
1✔
95
                return $this;
1✔
96
        }
97

98

99
        /** @return array<string, mixed> */
100
        public function getOptionAttributes(): array
101
        {
102
                return $this->optionAttributes;
×
103
        }
104
}
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