1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Form\Field\Base\AbstractField; |
9
|
|
|
use Yiisoft\Form\Field\Base\PlaceholderTrait; |
10
|
|
|
use Yiisoft\Html\Html; |
11
|
|
|
|
12
|
|
|
use function is_string; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Generates a text input tag for the given form attribute. |
16
|
|
|
* |
17
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#text-(type=text)-state-and-search-state-(type=search) |
18
|
|
|
*/ |
19
|
|
|
final class Text extends AbstractField |
20
|
|
|
{ |
21
|
|
|
use PlaceholderTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A boolean attribute that controls whether or not the user can edit the form control. |
25
|
|
|
* |
26
|
|
|
* @param bool $value Whether to allow the value to be edited by the user. |
27
|
|
|
* |
28
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly |
29
|
|
|
*/ |
30
|
|
|
public function readonly(bool $value = true): self |
31
|
|
|
{ |
32
|
|
|
$new = clone $this; |
33
|
|
|
$new->inputTagAttributes['readonly'] = $value; |
34
|
|
|
return $new; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A boolean attribute. When specified, the element is required. |
39
|
|
|
* |
40
|
|
|
* @param bool $value Whether the control is required for form submission. |
41
|
|
|
* |
42
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required |
43
|
|
|
*/ |
44
|
|
|
public function required(bool $value = true): self |
45
|
|
|
{ |
46
|
|
|
$new = clone $this; |
47
|
|
|
$new->inputTagAttributes['required'] = $value; |
48
|
|
|
return $new; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Name of form control to use for sending the element's directionality in form submission |
53
|
|
|
* |
54
|
|
|
* @param string $value Any string that is not empty. |
55
|
|
|
* |
56
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-dirname |
57
|
|
|
*/ |
58
|
3 |
|
public function dirname(string $value): self |
59
|
|
|
{ |
60
|
3 |
|
if (empty($value)) { |
61
|
1 |
|
throw new InvalidArgumentException('The value cannot be empty.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$new = clone $this; |
65
|
2 |
|
$new->inputTagAttributes['dirname'] = $value; |
66
|
2 |
|
return $new; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Maximum length of value. |
71
|
|
|
* |
72
|
|
|
* @param int $value A limit on the number of characters a user can input. |
73
|
|
|
* |
74
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength |
75
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength |
76
|
|
|
*/ |
77
|
2 |
|
public function maxlength(int $value): self |
78
|
|
|
{ |
79
|
2 |
|
$new = clone $this; |
80
|
2 |
|
$new->inputTagAttributes['maxlength'] = $value; |
81
|
2 |
|
return $new; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Minimum length of value. |
86
|
|
|
* |
87
|
|
|
* @param int $value A lower bound on the number of characters a user can input. |
88
|
|
|
* |
89
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength |
90
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength |
91
|
|
|
*/ |
92
|
2 |
|
public function minlength(int $value): self |
93
|
|
|
{ |
94
|
2 |
|
$new = clone $this; |
95
|
2 |
|
$new->inputTagAttributes['minlength'] = $value; |
96
|
2 |
|
return $new; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Pattern to be matched by the form control's value. |
101
|
|
|
* |
102
|
|
|
* @param string $value A regular expression against which the control's value. |
103
|
|
|
* |
104
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern |
105
|
|
|
*/ |
106
|
2 |
|
public function pattern(string $value): self |
107
|
|
|
{ |
108
|
2 |
|
$new = clone $this; |
109
|
2 |
|
$new->inputTagAttributes['pattern'] = $value; |
110
|
2 |
|
return $new; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* The size of the control. |
115
|
|
|
* |
116
|
|
|
* @param int $value The number of characters that allow the user to see while editing the element's value. |
117
|
|
|
* |
118
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size |
119
|
|
|
*/ |
120
|
2 |
|
public function size(int $value): self |
121
|
|
|
{ |
122
|
2 |
|
$new = clone $this; |
123
|
2 |
|
$new->inputTagAttributes['size'] = $value; |
124
|
2 |
|
return $new; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Identifies the element (or elements) that describes the object. |
129
|
|
|
* |
130
|
|
|
* @link https://w3c.github.io/aria/#aria-describedby |
131
|
|
|
*/ |
132
|
|
|
public function ariaDescribedBy(string $value): self |
133
|
|
|
{ |
134
|
|
|
$new = clone $this; |
135
|
|
|
$new->inputTagAttributes['aria-describedby'] = $value; |
136
|
|
|
return $new; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Defines a string value that labels the current element. |
141
|
|
|
* |
142
|
|
|
* @link https://w3c.github.io/aria/#aria-label |
143
|
|
|
*/ |
144
|
|
|
public function ariaLabel(string $value): self |
145
|
|
|
{ |
146
|
|
|
$new = clone $this; |
147
|
|
|
$new->inputTagAttributes['aria-label'] = $value; |
148
|
|
|
return $new; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the ID |
153
|
|
|
* attribute of a form element in the same document. |
154
|
|
|
* |
155
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
156
|
|
|
*/ |
157
|
|
|
public function form(string $value): self |
158
|
|
|
{ |
159
|
|
|
$new = clone $this; |
160
|
|
|
$new->inputTagAttributes['form'] = $value; |
161
|
|
|
return $new; |
162
|
|
|
} |
163
|
|
|
|
164
|
34 |
|
protected function generateInput(): string |
165
|
|
|
{ |
166
|
34 |
|
$value = $this->getAttributeValue(); |
167
|
|
|
|
168
|
34 |
|
if (!is_string($value) && $value !== null) { |
169
|
1 |
|
throw new InvalidArgumentException('Text widget must be a string or null value.'); |
170
|
|
|
} |
171
|
|
|
|
172
|
33 |
|
$tagAttributes = $this->getInputTagAttributes(); |
173
|
|
|
|
174
|
|
|
/** @psalm-suppress MixedArgumentTypeCoercion */ |
175
|
33 |
|
return Html::textInput($this->getInputName(), $value, $tagAttributes)->render(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|