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

nette / forms / 28395249751

29 Jun 2026 06:51PM UTC coverage: 93.369%. Remained the same
28395249751

Pull #353

github

web-flow
Merge cd11b2ef3 into 8c59b75ac
Pull Request #353: Form: getHttpData() return type reflects the $htmlName argument

2112 of 2262 relevant lines covered (93.37%)

0.93 hits per line

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

97.67
/src/Forms/Controls/TextBase.php
1
<?php declare(strict_types=1);
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 Nette\Forms\Form;
12
use Nette\Utils\Strings;
13
use Stringable;
14
use function get_debug_type, is_array, is_scalar, min, sprintf;
15

16

17
/**
18
 * Base for text-based controls (TextInput, TextArea) with nullable and empty-value support.
19
 */
20
abstract class TextBase extends BaseControl
21
{
22
        protected string $emptyValue = '';
23
        protected mixed $rawValue = '';
24
        private bool $nullable = false;
25

26

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

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

44

45
        /**
46
         * Returns the value, substituting empty string when it matches the empty value. Returns null when nullable is set and value is empty.
47
         */
48
        public function getValue(): mixed
49
        {
50
                $value = $this->value === Strings::trim($this->translate($this->emptyValue))
1✔
51
                        ? ''
1✔
52
                        : $this->value;
1✔
53
                return $this->nullable && $value === '' ? null : $value;
1✔
54
        }
55

56

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

66

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

72

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

82

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

91

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

101

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

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

113
                return $el;
1✔
114
        }
115

116

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

124

125
        /**
126
         * @param  (callable(Nette\Forms\Control, mixed): bool)|string  $validator
127
         * @return static
128
         */
129
        public function addRule(
1✔
130
                callable|string $validator,
131
                string|Stringable|null $errorMessage = null,
132
                mixed $arg = null,
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