/
Input.php
268 lines (243 loc) · 7.81 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
declare(strict_types=1);
namespace Yii\Extension\Simple\Forms;
use InvalidArgumentException;
use Yiisoft\Html\Html;
use Yiisoft\Validator\Rule\Required;
use function in_array;
/**
* A widget for rendering a input form.
*/
final class Input extends Widget
{
public const TYPE_BUTTON = 'button';
public const TYPE_CHECKBOX = 'checkbox';
public const TYPE_COLOR = 'color';
public const TYPE_DATE = 'date';
public const TYPE_DATETIME_LOCAL = 'datetime-local';
public const TYPE_EMAIL = 'email';
public const TYPE_FILE = 'file';
public const TYPE_HIDDEN = 'hidden';
public const TYPE_IMAGE = 'image';
public const TYPE_MONTH = 'month';
public const TYPE_NUMBER = 'number';
public const TYPE_PASSWORD = 'password';
public const TYPE_RADIO = 'radio';
public const TYPE_RANGE = 'range';
public const TYPE_RESET = 'reset';
public const TYPE_SEARCH = 'search';
public const TYPE_SUBMIT = 'submit';
public const TYPE_TEL = 'tel';
public const TYPE_TEXT = 'text';
public const TYPE_TIME = 'time';
public const TYPE_URL = 'url';
public const TYPE_WEEK = 'week';
public const TYPE_ALL = [
self::TYPE_BUTTON,
self::TYPE_CHECKBOX,
self::TYPE_COLOR,
self::TYPE_DATE,
self::TYPE_DATETIME_LOCAL,
self::TYPE_EMAIL,
self::TYPE_FILE,
self::TYPE_HIDDEN,
self::TYPE_IMAGE,
self::TYPE_MONTH,
self::TYPE_NUMBER,
self::TYPE_PASSWORD,
self::TYPE_RADIO,
self::TYPE_RANGE,
self::TYPE_RESET,
self::TYPE_SEARCH,
self::TYPE_SUBMIT,
self::TYPE_TEL,
self::TYPE_TEXT,
self::TYPE_TIME,
self::TYPE_URL,
self::TYPE_WEEK,
];
private string $id = '';
private bool $noPlaceholder = false;
private string $type = 'text';
/**
* Generates an input tag for the given form attribute.
*
* @return string the generated input tag.
*/
protected function run(): string
{
$new = clone $this;
if (empty($new->modelInterface) || empty($new->attribute)) {
throw new InvalidArgumentException(
'The widget must be configured with FormInterface::class and Attribute.',
);
}
$id = $new->getId($new->modelInterface->getFormName(), $new->attribute);
if ($id !== '') {
$new->attributes['id'] = $new->getId($new->modelInterface->getFormName(), $new->attribute);
}
if ($new->noPlaceholder === false) {
$new->setPlaceholder();
}
/** @var array */
$rules = $new->modelInterface->getRules();
/** @var object $rule */
foreach ($rules[$new->attribute] as $rule) {
if ($rule instanceof Required) {
$new = $new->required();
}
}
return
Html::input(
$new->type,
$new->getInputName($new->modelInterface->getFormName(), $new->attribute),
$new->modelInterface->getAttributeValue($new->getAttributeName($new->attribute)),
)->attributes($new->attributes)->render();
}
/**
* 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).
*
* @return static
*/
public function autofocus(): self
{
$new = clone $this;
$new->attributes['autofocus'] = true;
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.
*
* @return static
*/
public function disabled(): self
{
$new = clone $this;
$new->attributes['disabled'] = true;
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 static
*/
public function form(string $value): self
{
$new = clone $this;
$new->attributes['form'] = $value;
return $new;
}
/**
* Allows you to disable placeholder.
*
* @return static
*/
public function noPlaceholder(): self
{
$new = clone $this;
$new->noPlaceholder = true;
return $new;
}
/**
* Set custom error message when an input field is invalid.
*
* @param string $message
*
* @return static
*/
public function onInvalid(string $message): self
{
$new = clone $this;
$new->attributes['oninvalid'] = "this.setCustomValidity('$message')";
return $new;
}
/**
* It allows defining placeholder.
*
* @param string $value
*
* @return static
*/
public function placeholder(string $value): self
{
$new = clone $this;
$new->attributes['placeholder'] = $value;
return $new;
}
/**
* If it is required to fill in a value in order to submit the form.
*
* @return static
*/
public function required(): self
{
$new = clone $this;
$new->attributes['required'] = true;
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 static
*/
public function tabIndex(int $value): self
{
$new = clone $this;
$new->attributes['tabindex'] = $value;
return $new;
}
/**
* Type of the input control to use.
*
* @param string $value
*
* @return self
*/
public function type(string $value): self
{
if (!in_array($value, self::TYPE_ALL)) {
$values = implode('", "', self::TYPE_ALL);
throw new InvalidArgumentException("Invalid type. Valid values are: \"$values\".");
}
$new = clone $this;
$new->type = $value;
return $new;
}
private function setPlaceholder(): void
{
if (
!isset($this->attributes['placeholder']) &&
!(in_array($this->type, ['date', 'file', 'hidden', 'color'], true))
) {
$attributeName = $this->getAttributeName($this->attribute);
$this->attributes['placeholder'] = $this->modelInterface->getAttributeLabel($attributeName);
}
}
}