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

nette / forms / 28608018984

02 Jul 2026 05:08PM UTC coverage: 93.512% (+0.2%) from 93.28%
28608018984

push

github

dg
x

2854 of 3052 relevant lines covered (93.51%)

0.94 hits per line

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

97.06
/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\Utils\Strings;
12
use Stringable;
13
use function get_debug_type, is_array, is_scalar, min, sprintf;
14

15

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

25

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

35
                $this->value = $value;
1✔
36
                $this->rawValue = (string) $value;
1✔
37
                return $this;
1✔
38
        }
39

40

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

52

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

62

63
        public function isNullable(): bool
64
        {
65
                return $this->nullable;
1✔
66
        }
67

68

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

78

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

87

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

97

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

105
                if (isset($el->placeholder)) {
1✔
106
                        $el->placeholder = $this->translate($el->placeholder);
1✔
107
                }
108

109
                return $el;
1✔
110
        }
111

112

113
        protected function getRenderedValue(): ?string
114
        {
115
                return $this->rawValue === ''
1✔
116
                        ? ($this->emptyValue === '' ? null : $this->translate($this->emptyValue))
1✔
117
                        : $this->rawValue;
1✔
118
        }
119

120

121
        protected function getRuleAttributeNames(): array
122
        {
123
                return ['maxlength'];
1✔
124
        }
125
}
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