• 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

94.12
/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 Nette\Forms\Validation;
13
use Stringable;
14
use function in_array, is_scalar, max, min;
15

16

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

29

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

35

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

45

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

53

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

62

63
        /** @param  Validation\Validator|Validation\ControlValidator|(callable(Nette\Forms\Control, mixed): bool)|string  $validator */
64
        public function addRule(
1✔
65
                Validation\Validator|Validation\ControlValidator|callable|string $validator,
66
                string|Stringable|null $errorMessage = null,
67
                mixed $arg = null,
68
        ): static
69
        {
70
                if ($this->getRules()->containsMutating()) {
1✔
71
                        // the value changes on the server, so HTML attributes must not pre-validate the raw input
72
                        return parent::addRule($validator, $errorMessage, $arg);
1✔
73
                }
74

75
                $type = match (true) {
1✔
76
                        $validator === Form::Email, $validator instanceof Validation\EmailValidator => 'email',
1✔
77
                        $validator === Form::URL, $validator instanceof Validation\UrlValidator => 'url',
1✔
78
                        $validator === Form::Integer, $validator instanceof Validation\IntegerValidator => 'number',
1✔
79
                        default => null,
1✔
80
                };
81
                if ($type !== null && $this->control->type === null) {
1✔
82
                        $this->control->type = $type;
1✔
83
                }
84

85
                return parent::addRule($validator, $errorMessage, $arg);
1✔
86
        }
87

88

89
        protected function getRuleAttributeNames(): array
90
        {
91
                $names = parent::getRuleAttributeNames();
1✔
92
                if (in_array($this->control->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], strict: true)) {
1✔
93
                        $names[] = 'min';
1✔
94
                        $names[] = 'max';
1✔
95
                }
96

97
                if (in_array($this->control->type, [null, 'text', 'search', 'tel', 'url', 'email', 'password'], strict: true)) {
1✔
98
                        $names[] = 'pattern';
1✔
99
                }
100

101
                return $names;
1✔
102
        }
103
}
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