1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\FormModelInterface; |
8
|
|
|
use Yiisoft\Widget\Widget; |
9
|
|
|
|
10
|
|
|
final class TextInput extends Widget |
11
|
|
|
{ |
12
|
|
|
private FormModelInterface $data; |
13
|
|
|
private string $attribute; |
14
|
|
|
private array $options = []; |
15
|
|
|
private bool $noPlaceHolder = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Generates a text input tag for the given form attribute. |
19
|
|
|
* |
20
|
|
|
* @return string the generated input tag. |
21
|
|
|
*/ |
22
|
|
|
public function run(): string |
23
|
|
|
{ |
24
|
|
|
return Input::widget() |
25
|
|
|
->type('text') |
|
|
|
|
26
|
|
|
->config($this->data, $this->attribute, $this->options) |
27
|
|
|
->noPlaceHolder($this->noPlaceHolder) |
28
|
|
|
->run(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set form model, name and options for the widget. |
33
|
|
|
* |
34
|
|
|
* @param FormModelInterface $data Form model. |
35
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
36
|
|
|
* @param array $options The HTML attributes for the widget container tag. |
37
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
38
|
|
|
* |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
42
|
|
|
{ |
43
|
|
|
$new = clone $this; |
44
|
|
|
$new->data = $data; |
45
|
|
|
$new->attribute = $attribute; |
46
|
|
|
$new->options = $options; |
47
|
|
|
return $new; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Focus on the control (put cursor into it) when the page loads. |
52
|
|
|
* Only one form element could be in focus at the same time. |
53
|
|
|
* |
54
|
|
|
* @param bool $value |
55
|
|
|
* |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
|
|
public function autofocus(bool $value = true): self |
59
|
|
|
{ |
60
|
|
|
$new = clone $this; |
61
|
|
|
$new->options['autofocus'] = $value; |
62
|
|
|
return $new; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set whether the element is disabled or not. |
67
|
|
|
* |
68
|
|
|
* If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out |
69
|
|
|
* text. |
70
|
|
|
* If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event |
71
|
|
|
* will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as |
72
|
|
|
* this will suggest you can set it to false to enable the element again, which is not the case. |
73
|
|
|
* |
74
|
|
|
* @param bool $value |
75
|
|
|
* |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function disabled(bool $value = true): self |
79
|
|
|
{ |
80
|
|
|
$new = clone $this; |
81
|
|
|
$new->options['disabled'] = $value; |
82
|
|
|
return $new; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the id |
87
|
|
|
* attribute of a {@see Form} element in the same document. |
88
|
|
|
* |
89
|
|
|
* @param string $value |
90
|
|
|
* |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
|
|
public function form(string $value): self |
94
|
|
|
{ |
95
|
|
|
$new = clone $this; |
96
|
|
|
$new->options['form'] = $value; |
97
|
|
|
return $new; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The minimum number of characters (as UTF-16 code units) the user can enter into the text input. |
102
|
|
|
* |
103
|
|
|
* This must be an non-negative integer value smaller than or equal to the value specified by maxlength. |
104
|
|
|
* If no minlength is specified, or an invalid value is specified, the text input has no minimum length. |
105
|
|
|
* |
106
|
|
|
* @param int $value |
107
|
|
|
* |
108
|
|
|
* @return self |
109
|
|
|
*/ |
110
|
|
|
public function minlength(int $value): self |
111
|
|
|
{ |
112
|
|
|
$new = clone $this; |
113
|
|
|
$new->options['minlength'] = $value; |
114
|
|
|
return $new; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into |
119
|
|
|
* an tag input. |
120
|
|
|
* |
121
|
|
|
* If no maxlength is specified, or an invalid value is specified, the tag input has no maximum length. |
122
|
|
|
* |
123
|
|
|
* @param int $value |
124
|
|
|
* |
125
|
|
|
* @return self |
126
|
|
|
*/ |
127
|
|
|
public function maxlength(int $value): self |
128
|
|
|
{ |
129
|
|
|
$new = clone $this; |
130
|
|
|
$new->options['maxlength'] = $value; |
131
|
|
|
return $new; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Allows you to disable placeholder. |
136
|
|
|
* |
137
|
|
|
* @param bool $value |
138
|
|
|
* |
139
|
|
|
* @return self |
140
|
|
|
*/ |
141
|
|
|
public function noPlaceholder(bool $value = true): self |
142
|
|
|
{ |
143
|
|
|
$new = clone $this; |
144
|
|
|
$new->noPlaceHolder = $value; |
145
|
|
|
return $new; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* The pattern attribute, when specified, is a regular expression that the input's value must match in order for |
150
|
|
|
* the value to pass constraint validation. It must be a valid JavaScript regular expression, as used by the |
151
|
|
|
* RegExp type. |
152
|
|
|
* |
153
|
|
|
* @param string $value |
154
|
|
|
* |
155
|
|
|
* @return self |
156
|
|
|
*/ |
157
|
|
|
public function pattern(string $value): self |
158
|
|
|
{ |
159
|
|
|
$new = clone $this; |
160
|
|
|
$new->options['pattern'] = $value; |
161
|
|
|
return $new; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* It allows defining placeholder. |
166
|
|
|
* |
167
|
|
|
* @param string $value |
168
|
|
|
* |
169
|
|
|
* @return self |
170
|
|
|
*/ |
171
|
|
|
public function placeholder(string $value): self |
172
|
|
|
{ |
173
|
|
|
$new = clone $this; |
174
|
|
|
$new->options['placeholder'] = $value; |
175
|
|
|
return $new; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* A Boolean attribute which, if present, means this field cannot be edited by the user. |
180
|
|
|
* Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value |
181
|
|
|
* property. |
182
|
|
|
* |
183
|
|
|
* @param bool $value |
184
|
|
|
* |
185
|
|
|
* @return self |
186
|
|
|
*/ |
187
|
|
|
public function readOnly(bool $value = true): self |
188
|
|
|
{ |
189
|
|
|
$new = clone $this; |
190
|
|
|
$new->options['readonly'] = $value; |
191
|
|
|
return $new; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* The size attribute is a numeric value indicating how many characters wide the input field should be. |
196
|
|
|
* |
197
|
|
|
* The value must be a number greater than zero, and the default value is 20. |
198
|
|
|
* |
199
|
|
|
* @param int $value |
200
|
|
|
* |
201
|
|
|
* @return self |
202
|
|
|
* |
203
|
|
|
* Note: This does not set a limit on how many characters the user can enter into the field. |
204
|
|
|
* - It only specifies approximately how many can be seen at a time. |
205
|
|
|
* - To set an upper limit on the length of the input data, use the maxlength attribute. |
206
|
|
|
*/ |
207
|
|
|
public function size(int $value): self |
208
|
|
|
{ |
209
|
|
|
$new = clone $this; |
210
|
|
|
$new->options['size'] = $value; |
211
|
|
|
return $new; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Spellcheck is a global attribute which is used to indicate whether or not to enable spell checking for an |
216
|
|
|
* element. |
217
|
|
|
* |
218
|
|
|
* @param bool $value |
219
|
|
|
* |
220
|
|
|
* @return self |
221
|
|
|
*/ |
222
|
|
|
public function spellcheck(bool $value = true): self |
223
|
|
|
{ |
224
|
|
|
$new = clone $this; |
225
|
|
|
$new->options['spellcheck'] = $value; |
226
|
|
|
return $new; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* If it is required to fill in a value in order to submit the form. |
231
|
|
|
* |
232
|
|
|
* @param bool $value |
233
|
|
|
* |
234
|
|
|
* @return self |
235
|
|
|
*/ |
236
|
|
|
public function required(bool $value = true): self |
237
|
|
|
{ |
238
|
|
|
$new = clone $this; |
239
|
|
|
$new->options['required'] = $value; |
240
|
|
|
return $new; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* The tabindex global attribute indicates that its element can be focused, and where it participates in sequential |
245
|
|
|
* keyboard navigation (usually with the Tab key, hence the name). |
246
|
|
|
* |
247
|
|
|
* It accepts an integer as a value, with different results depending on the integer's value: |
248
|
|
|
* |
249
|
|
|
* - A negative value (usually tabindex="-1") means that the element is not reachable via sequential keyboard |
250
|
|
|
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets |
251
|
|
|
* with JavaScript. |
252
|
|
|
* - tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is |
253
|
|
|
* defined by the document's source order. |
254
|
|
|
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order |
255
|
|
|
* defined by the value of the number. That is, tabindex="4" is focused before tabindex="5", but after tabindex="3". |
256
|
|
|
* |
257
|
|
|
* @param int $value |
258
|
|
|
* |
259
|
|
|
* @return self |
260
|
|
|
*/ |
261
|
|
|
public function tabIndex(int $value = 0): self |
262
|
|
|
{ |
263
|
|
|
$new = clone $this; |
264
|
|
|
$new->options['tabindex'] = $value; |
265
|
|
|
return $new; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* The title global attribute contains text representing advisory information related to the element it belongs to. |
270
|
|
|
* |
271
|
|
|
* @param string $value |
272
|
|
|
* |
273
|
|
|
* @return self |
274
|
|
|
*/ |
275
|
|
|
public function title(string $value): self |
276
|
|
|
{ |
277
|
|
|
$new = clone $this; |
278
|
|
|
$new->options['title'] = $value; |
279
|
|
|
return $new; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|