-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
TextInput.php
114 lines (92 loc) 路 2.66 KB
/
TextInput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Forms\Controls;
use Nette;
use Nette\Forms\Form;
use Stringable;
/**
* Single line text input control.
*/
class TextInput extends TextBase
{
public function __construct(string|Stringable|null $label = null, ?int $maxLength = null)
{
parent::__construct($label);
$this->control->maxlength = $maxLength;
$this->setOption('type', 'text');
}
public function loadHttpData(): void
{
$this->setValue($this->getHttpData(Form::DataLine));
}
/**
* Changes control's type attribute.
*/
public function setHtmlType(string $type): static
{
$this->control->type = $type;
return $this;
}
/**
* @deprecated use setHtmlType()
*/
public function setType(string $type): static
{
return $this->setHtmlType($type);
}
public function getControl(): Nette\Utils\Html
{
return parent::getControl()->addAttributes([
'value' => $this->control->type === 'password' ? $this->control->value : $this->getRenderedValue(),
'type' => $this->control->type ?: 'text',
]);
}
public function addRule(
callable|string $validator,
string|Stringable|null $errorMessage = null,
mixed $arg = null,
): static
{
foreach ($this->getRules() as $rule) {
if (!$rule->canExport() && !$rule->branch) {
return parent::addRule($validator, $errorMessage, $arg);
}
}
if ($this->control->type === null && in_array($validator, [Form::Email, Form::URL, Form::Integer], true)) {
$types = [Form::Email => 'email', Form::URL => 'url', Form::Integer => 'number'];
$this->control->type = $types[$validator];
} elseif (
in_array($validator, [Form::Min, Form::Max, Form::Range], true)
&& in_array($this->control->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], true)
) {
if ($validator === Form::Min) {
$range = [$arg, null];
} elseif ($validator === Form::Max) {
$range = [null, $arg];
} else {
$range = $arg;
}
if (isset($range[0]) && is_scalar($range[0])) {
$this->control->min = isset($this->control->min)
? max($this->control->min, $range[0])
: $range[0];
}
if (isset($range[1]) && is_scalar($range[1])) {
$this->control->max = isset($this->control->max)
? min($this->control->max, $range[1])
: $range[1];
}
} elseif (
$validator === Form::Pattern
&& is_scalar($arg)
&& in_array($this->control->type, [null, 'text', 'search', 'tel', 'url', 'email', 'password'], true)
) {
$this->control->pattern = $arg;
}
return parent::addRule($validator, $errorMessage, $arg);
}
}