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

nette / forms / 21851967935

10 Feb 2026 04:40AM UTC coverage: 93.076% (+0.2%) from 92.892%
21851967935

push

github

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

2070 of 2224 relevant lines covered (93.08%)

0.93 hits per line

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

97.62
/src/Forms/Controls/TextBase.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 Nette\Forms\Form;
14
use Nette\Utils\Strings;
15
use Stringable;
16
use function get_debug_type, is_array, is_scalar, min, sprintf;
17

18

19
/**
20
 * Implements the basic functionality common to text input controls.
21
 */
22
abstract class TextBase extends BaseControl
23
{
24
        protected string $emptyValue = '';
25
        protected mixed $rawValue = '';
26
        private bool $nullable = false;
27

28

29
        /**
30
         * Sets control's value.
31
         * @internal
32
         */
33
        public function setValue($value): static
34
        {
35
                if ($value === null) {
1✔
36
                        $value = '';
1✔
37
                } elseif (!is_scalar($value) && !$value instanceof Stringable) {
1✔
38
                        throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->getName()));
1✔
39
                }
40

41
                $this->value = $value;
1✔
42
                $this->rawValue = (string) $value;
1✔
43
                return $this;
1✔
44
        }
45

46

47
        /**
48
         * Returns control's value.
49
         */
50
        public function getValue(): mixed
51
        {
52
                $value = $this->value === Strings::trim($this->translate($this->emptyValue))
1✔
53
                        ? ''
1✔
54
                        : $this->value;
1✔
55
                return $this->nullable && $value === '' ? null : $value;
1✔
56
        }
57

58

59
        /**
60
         * Sets whether getValue() returns null instead of empty string.
61
         */
62
        public function setNullable(bool $value = true): static
1✔
63
        {
64
                $this->nullable = $value;
1✔
65
                return $this;
1✔
66
        }
67

68

69
        public function isNullable(): bool
70
        {
71
                return $this->nullable;
1✔
72
        }
73

74

75
        /**
76
         * Sets the special value which is treated as empty string.
77
         */
78
        public function setEmptyValue(string $value): static
1✔
79
        {
80
                $this->emptyValue = $value;
1✔
81
                return $this;
1✔
82
        }
83

84

85
        /**
86
         * Returns the special value which is treated as empty string.
87
         */
88
        public function getEmptyValue(): string
89
        {
90
                return $this->emptyValue;
×
91
        }
92

93

94
        /**
95
         * Sets the maximum number of allowed characters.
96
         */
97
        public function setMaxLength(?int $length): static
1✔
98
        {
99
                $this->control->maxlength = $length;
1✔
100
                return $this;
1✔
101
        }
102

103

104
        public function getControl(): Nette\Utils\Html
105
        {
106
                $el = parent::getControl();
1✔
107
                if ($this->emptyValue !== '') {
1✔
108
                        $el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue));
1✔
109
                }
110

111
                if (isset($el->placeholder)) {
1✔
112
                        $el->placeholder = $this->translate($el->placeholder);
1✔
113
                }
114

115
                return $el;
1✔
116
        }
117

118

119
        protected function getRenderedValue(): ?string
120
        {
121
                return $this->rawValue === ''
1✔
122
                        ? ($this->emptyValue === '' ? null : $this->translate($this->emptyValue))
1✔
123
                        : $this->rawValue;
1✔
124
        }
125

126

127
        /** @param  (callable(self): bool)|string  $validator */
128
        public function addRule(
1✔
129
                callable|string $validator,
130
                string|Stringable|null $errorMessage = null,
131
                mixed $arg = null,
132
        ): static
133
        {
134
                foreach ($this->getRules() as $rule) {
1✔
135
                        if (!$rule->canExport() && !$rule->branch) {
1✔
136
                                return parent::addRule($validator, $errorMessage, $arg);
1✔
137
                        }
138
                }
139

140
                if ($validator === Form::Length || $validator === Form::MaxLength) {
1✔
141
                        $tmp = is_array($arg) ? $arg[1] : $arg;
1✔
142
                        if (is_scalar($tmp)) {
1✔
143
                                $this->control->maxlength = isset($this->control->maxlength)
1✔
144
                                        ? min($this->control->maxlength, $tmp)
1✔
145
                                        : $tmp;
1✔
146
                        }
147
                }
148

149
                return parent::addRule($validator, $errorMessage, $arg);
1✔
150
        }
151
}
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