• 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.56
/src/Forms/Controls/TextInput.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 Stringable;
15
use function in_array, is_scalar, max, min;
16

17

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

30

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

36

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

46

47
        /**
48
         * @deprecated  use setHtmlType()
49
         */
50
        public function setType(string $type): static
51
        {
52
                return $this->setHtmlType($type);
×
53
        }
54

55

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

64

65
        public function addRule(
1✔
66
                callable|string $validator,
67
                string|Stringable|null $errorMessage = null,
68
                mixed $arg = null,
69
        ): static
70
        {
71
                foreach ($this->getRules() as $rule) {
1✔
72
                        if (!$rule->canExport() && !$rule->branch) {
1✔
73
                                return parent::addRule($validator, $errorMessage, $arg);
1✔
74
                        }
75
                }
76

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

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

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

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

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

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