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

nette / forms / 26452888331

26 May 2026 02:01PM UTC coverage: 93.307% (+0.07%) from 93.241%
26452888331

push

github

dg
added CLAUDE.md

2105 of 2256 relevant lines covered (93.31%)

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 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 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
         * @return mixed
48
         */
49
        public function getValue(): mixed
50
        {
51
                $value = $this->value === Strings::trim($this->translate($this->emptyValue))
1✔
52
                        ? ''
1✔
53
                        : $this->value;
1✔
54
                return $this->nullable && $value === '' ? null : $value;
1✔
55
        }
56

57

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

67

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

73

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

83

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

92

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

102

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

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

114
                return $el;
1✔
115
        }
116

117

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

125

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

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

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