generated from yiisoft/package-template
/
Input.php
249 lines (223 loc) · 6.98 KB
/
Input.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
declare(strict_types=1);
namespace Yiisoft\Form\Widget;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Form\FormModelInterface;
use Yiisoft\Form\Helper\HtmlForm;
use Yiisoft\Html\Html;
use Yiisoft\Widget\Widget;
use function in_array;
final class Input extends Widget
{
private ?string $id = null;
private FormModelInterface $data;
private string $attribute;
private array $options = [];
private string $charset = 'UTF-8';
private bool $noPlaceholder = false;
private string $type;
/**
* Generates an input tag for the given form attribute.
*
* @return string the generated input tag.
*/
public function run(): string
{
$new = clone $this;
if ($new->noPlaceholder === false) {
$new->setPlaceholder();
}
if (!empty($new->getId())) {
$new->options['id'] = $new->getId();
}
return Html::input($new->type, $new->getName(), $new->getValue(), $new->options);
}
/**
* Set form model, name and options for the widget.
*
* @param FormModelInterface $data Form model.
* @param string $attribute Form model property this widget is rendered for.
* @param array $options The HTML attributes for the widget container tag.
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*
* @return self
*/
public function config(FormModelInterface $data, string $attribute, array $options = []): self
{
$new = clone $this;
$new->data = $data;
$new->attribute = $attribute;
$new->options = $options;
return $new;
}
/**
* Focus on the control (put cursor into it) when the page loads.
* Only one form element could be in focus at the same time.
*
* It cannot be applied if the type attribute has a hidden value (that is, you cannot automatically set the cursor
* to a hidden control).
*
* @param bool $value
*
* @return self
*/
public function autofocus(bool $value = true): self
{
$new = clone $this;
$new->options['autofocus'] = $value;
return $new;
}
/**
* Set the character set used to generate the widget id. See {@see HtmlForm::getInputId()}.
*
* @param string $value
*
* @return self
*/
public function charset(string $value): self
{
$new = clone $this;
$new->charset = $value;
return $new;
}
/**
* Set whether the element is disabled or not.
*
* If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out
* text.
* If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event
* will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as
* this will suggest you can set it to false to enable the element again, which is not the case.
*
* @param bool $value
*
* @return self
*/
public function disabled(bool $value = true): self
{
$new = clone $this;
$new->options['disabled'] = $value;
return $new;
}
/**
* Specifies the form element the tag input element belongs to. The value of this attribute must be the id
* attribute of a {@see Form} element in the same document.
*
* @param string $value
*
* @return self
*/
public function form(string $value): self
{
$new = clone $this;
$new->options['form'] = $value;
return $new;
}
/**
* Allows you to disable placeholder.
*
* @param bool $value
*
* @return self
*/
public function noPlaceholder(bool $value = true): self
{
$new = clone $this;
$new->noPlaceholder = $value;
return $new;
}
/**
* It allows defining placeholder.
*
* @param string $value
*
* @return self
*/
public function placeholder(string $value): self
{
$new = clone $this;
$new->options['placeholder'] = $value;
return $new;
}
/**
* If it is required to fill in a value in order to submit the form.
*
* @param bool $value
*
* @return self
*/
public function required(bool $value = true): self
{
$new = clone $this;
$new->options['required'] = $value;
return $new;
}
/**
* The tabindex global attribute indicates that its element can be focused, and where it participates in sequential
* keyboard navigation (usually with the Tab key, hence the name).
*
* It accepts an integer as a value, with different results depending on the integer's value:
*
* - A negative value (usually tabindex="-1") means that the element is not reachable via sequential keyboard
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
* with JavaScript.
* - tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is
* defined by the document's source order.
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order
* defined by the value of the number. That is, tabindex="4" is focused before tabindex="5", but after tabindex="3".
*
* @param int $value
*
* @return self
*/
public function tabIndex(int $value = 0): self
{
$new = clone $this;
$new->options['tabindex'] = $value;
return $new;
}
/**
* Type of the input control to use.
*
* @param string $value
*
* @return self
*/
public function type(string $value): self
{
$new = clone $this;
$new->type = $value;
return $new;
}
private function getId(): string
{
$id = $this->options['id'] ?? $this->id;
if ($id === null) {
$id = HtmlForm::getInputId($this->data, $this->attribute, $this->charset);
}
return $id !== false ? (string) $id : '';
}
private function getName(): string
{
return ArrayHelper::remove($this->options, 'name', HtmlForm::getInputName($this->data, $this->attribute));
}
private function getValue()
{
$value = HtmlForm::getAttributeValue($this->data, $this->attribute);
if ($value !== null && is_scalar($value)) {
$value = (string)$value;
}
return ArrayHelper::remove(
$this->options,
'value',
$value
);
}
private function setPlaceholder(): void
{
if (!isset($this->options['placeholder']) && !(in_array($this->type, ['date', 'file', 'hidden', 'color'], true))) {
$attributeName = HtmlForm::getAttributeName($this->attribute);
$this->options['placeholder'] = $this->data->attributeLabel($attributeName);
}
}
}