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

nette / forms / 15763260878

19 Jun 2025 05:19PM UTC coverage: 93.011%. Remained the same
15763260878

push

github

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

2076 of 2232 relevant lines covered (93.01%)

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

59

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

69

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

75

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

85

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

94

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

104

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

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

116
                return $el;
1✔
117
        }
118

119

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

127

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

© 2025 Coveralls, Inc