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

nette / forms / 40630207392

28 Oct 2026 04:03AM UTC coverage: 92.13% (-1.2%) from 93.369%
40630207392

push

github

dg
Helpers::tryEnumConversion() no longer throws on incompatible input

When mapping a text/hidden control onto a backed-enum property, the raw
submitted value was passed straight to BackedEnum::from(). Under strict_types
that rejects any mismatch: an int-backed enum given a non-numeric string (e.g.
an empty field) raised TypeError, an out-of-range value raised ValueError.

The method is now total: it converts only when a matching case exists - coercing
numeric strings to the int backing type so int-backed enums map from form input
at all - and returns the value unchanged otherwise.

This only makes the helper itself safe; it does not add enum-membership
validation. A required enum property fed an invalid value still fails later at
the typed assignment in the target object's constructor, which is generic
typed-mapping behaviour, not specific to enums.

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

79 existing lines in 16 files now uncovered.

1955 of 2122 relevant lines covered (92.13%)

0.92 hits per line

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

93.1
/src/Forms/Controls/MultiSelectBox.php
1
<?php declare(strict_types=1);
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
namespace Nette\Forms\Controls;
9

10
use Nette;
11
use function is_array;
12

13

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

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

25

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

33

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

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

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

61

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

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

78

79
        /**
80
         * @param  array<string, mixed>  $attributes
81
         * @deprecated use setOptionAttribute()
82
         */
83
        public function addOptionAttributes(array $attributes): static
84
        {
85
                $this->optionAttributes = $attributes + $this->optionAttributes;
1 ✔
86
                return $this;
1 ✔
87
        }
88

89

90
        /**
91
         * Sets an attribute on all <option> elements. Use trailing ':' for per-item values or '?' for boolean attributes.
92
         */
93
        public function setOptionAttribute(string $name, mixed $value = true): static
94
        {
95
                $this->optionAttributes[$name] = $value;
1 ✔
96
                return $this;
1 ✔
97
        }
98

99

100
        /**
101
         * Returns all option attributes.
102
         * @return array<string, mixed>
103
         */
104
        public function getOptionAttributes(): array
105
        {
106
                return $this->optionAttributes;
×
107
        }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc