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

nette / forms / 28588025136

02 Jul 2026 11:28AM UTC coverage: 93.105% (+0.4%) from 92.701%
28588025136

push

github

dg
added Repeater example

2363 of 2538 relevant lines covered (93.1%)

0.93 hits per line

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

95.35
/src/Forms/Controls/TextInput.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 Stringable;
13
use function in_array, is_scalar, max, min;
14

15

16
/**
17
 * Single line text input control.
18
 */
19
class TextInput extends TextBase
20
{
21
        public function __construct(string|Stringable|null $label = null, ?int $maxLength = null)
1✔
22
        {
23
                parent::__construct($label);
1✔
24
                $this->control->maxlength = $maxLength;
1✔
25
                $this->setOption('type', 'text');
1✔
26
        }
1✔
27

28

29
        public function loadHttpData(): void
30
        {
31
                $this->setValue($this->getSubmittedValue(Form::DataLine));
1✔
32
        }
1✔
33

34

35
        /**
36
         * Changes control's type attribute.
37
         */
38
        public function setHtmlType(string $type): static
1✔
39
        {
40
                $this->control->type = $type;
1✔
41
                return $this;
1✔
42
        }
43

44

45
        #[\Deprecated('use setHtmlType()')]
46
        public function setType(string $type): static
47
        {
48
                trigger_error(__METHOD__ . '() was renamed to setHtmlType()', E_USER_DEPRECATED);
×
49
                return $this->setHtmlType($type);
×
50
        }
51

52

53
        public function getControl(): Nette\Utils\Html
54
        {
55
                return parent::getControl()->addAttributes([
1✔
56
                        'value' => $this->control->type === 'password' ? $this->control->value : $this->getRenderedValue(),
1✔
57
                        'type' => $this->control->type ?? 'text',
1✔
58
                ]);
59
        }
60

61

62
        /** @param  (callable(Nette\Forms\Control, mixed): bool)|string  $validator */
63
        public function addRule(
1✔
64
                callable|string $validator,
65
                string|Stringable|null $errorMessage = null,
66
                mixed $arg = null,
67
        ): static
68
        {
69
                foreach ($this->getRules() as $rule) {
1✔
70
                        if (!$rule->canExport() && !$rule->branch) {
1✔
71
                                return parent::addRule($validator, $errorMessage, $arg);
1✔
72
                        }
73
                }
74

75
                if ($this->control->type === null && in_array($validator, [Form::Email, Form::URL, Form::Integer], strict: true)) {
1✔
76
                        $types = [Form::Email => 'email', Form::URL => 'url', Form::Integer => 'number'];
1✔
77
                        $this->control->type = $types[$validator];
1✔
78

79
                } elseif (
80
                        in_array($validator, [Form::Min, Form::Max, Form::Range], strict: true)
1✔
81
                        && in_array($this->control->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], strict: true)
1✔
82
                ) {
83
                        if ($validator === Form::Min) {
1✔
84
                                $range = [$arg, null];
1✔
85
                        } elseif ($validator === Form::Max) {
1✔
86
                                $range = [null, $arg];
1✔
87
                        } else {
88
                                $range = $arg;
1✔
89
                        }
90

91
                        if (isset($range[0]) && is_scalar($range[0])) {
1✔
92
                                $this->control->min = isset($this->control->min)
1✔
93
                                        ? max($this->control->min, $range[0])
1✔
94
                                        : $range[0];
1✔
95
                        }
96

97
                        if (isset($range[1]) && is_scalar($range[1])) {
1✔
98
                                $this->control->max = isset($this->control->max)
1✔
99
                                        ? min($this->control->max, $range[1])
1✔
100
                                        : $range[1];
1✔
101
                        }
102

103
                } elseif (
104
                        $validator === Form::Pattern
1✔
105
                        && is_scalar($arg)
1✔
106
                        && in_array($this->control->type, [null, 'text', 'search', 'tel', 'url', 'email', 'password'], strict: true)
1✔
107
                ) {
108
                        $this->control->pattern = (string) $arg;
1✔
109
                }
110

111
                return parent::addRule($validator, $errorMessage, $arg);
1✔
112
        }
113
}
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