|
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 PasswordInput 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 password input tag for the given form attribute. |
|
19
|
|
|
* |
|
20
|
|
|
* @return string the generated input tag, |
|
21
|
|
|
*/ |
|
22
|
16 |
|
public function run(): string |
|
23
|
|
|
{ |
|
24
|
16 |
|
return Input::widget() |
|
25
|
16 |
|
->type('password') |
|
|
|
|
|
|
26
|
16 |
|
->config($this->data, $this->attribute, $this->options) |
|
27
|
16 |
|
->noPlaceholder($this->noPlaceholder) |
|
28
|
16 |
|
->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.See |
|
37
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
|
38
|
|
|
* |
|
39
|
|
|
* @return self |
|
40
|
|
|
*/ |
|
41
|
16 |
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
|
42
|
|
|
{ |
|
43
|
16 |
|
$new = clone $this; |
|
44
|
16 |
|
$new->data = $data; |
|
45
|
16 |
|
$new->attribute = $attribute; |
|
46
|
16 |
|
$new->options = $options; |
|
47
|
16 |
|
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
|
1 |
|
public function autofocus(bool $value = true): self |
|
59
|
|
|
{ |
|
60
|
1 |
|
$new = clone $this; |
|
61
|
1 |
|
$new->options['autofocus'] = $value; |
|
62
|
1 |
|
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
|
1 |
|
public function disabled(bool $value = true): self |
|
79
|
|
|
{ |
|
80
|
1 |
|
$new = clone $this; |
|
81
|
1 |
|
$new->options['disabled'] = $value; |
|
82
|
1 |
|
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
|
1 |
|
public function form(string $value): self |
|
94
|
|
|
{ |
|
95
|
1 |
|
$new = clone $this; |
|
96
|
1 |
|
$new->options['form'] = $value; |
|
97
|
1 |
|
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
|
1 |
|
public function minlength(int $value): self |
|
111
|
|
|
{ |
|
112
|
1 |
|
$new = clone $this; |
|
113
|
1 |
|
$new->options['minlength'] = $value; |
|
114
|
1 |
|
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
|
1 |
|
public function maxlength(int $value): self |
|
128
|
|
|
{ |
|
129
|
1 |
|
$new = clone $this; |
|
130
|
1 |
|
$new->options['maxlength'] = $value; |
|
131
|
1 |
|
return $new; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Allows you to disable placeholder. |
|
136
|
|
|
* |
|
137
|
|
|
* @param bool $value |
|
138
|
|
|
* |
|
139
|
|
|
* @return self |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function noPlaceholder(bool $value = true): self |
|
142
|
|
|
{ |
|
143
|
1 |
|
$new = clone $this; |
|
144
|
1 |
|
$new->noPlaceholder = $value; |
|
145
|
1 |
|
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
|
1 |
|
public function pattern(string $value): self |
|
158
|
|
|
{ |
|
159
|
1 |
|
$new = clone $this; |
|
160
|
1 |
|
$new->options['pattern'] = $value; |
|
161
|
1 |
|
return $new; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* It allows defining placeholder. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $value |
|
168
|
|
|
* |
|
169
|
|
|
* @return self |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function placeholder(string $value): self |
|
172
|
|
|
{ |
|
173
|
1 |
|
$new = clone $this; |
|
174
|
1 |
|
$new->options['placeholder'] = $value; |
|
175
|
1 |
|
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
|
1 |
|
public function readOnly(bool $value = true): self |
|
188
|
|
|
{ |
|
189
|
1 |
|
$new = clone $this; |
|
190
|
1 |
|
$new->options['readonly'] = $value; |
|
191
|
1 |
|
return $new; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* If it is required to fill in a value in order to submit the form. |
|
196
|
|
|
* |
|
197
|
|
|
* @param bool $value |
|
198
|
|
|
* |
|
199
|
|
|
* @return self |
|
200
|
|
|
*/ |
|
201
|
1 |
|
public function required(bool $value = true): self |
|
202
|
|
|
{ |
|
203
|
1 |
|
$new = clone $this; |
|
204
|
1 |
|
$new->options['required'] = $value; |
|
205
|
1 |
|
return $new; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* The tabindex global attribute indicates that its element can be focused, and where it participates in sequential |
|
210
|
|
|
* keyboard navigation (usually with the Tab key, hence the name). |
|
211
|
|
|
* |
|
212
|
|
|
* It accepts an integer as a value, with different results depending on the integer's value: |
|
213
|
|
|
* |
|
214
|
|
|
* - A negative value (usually tabindex="-1") means that the element is not reachable via sequential keyboard |
|
215
|
|
|
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets |
|
216
|
|
|
* with JavaScript. |
|
217
|
|
|
* - tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is |
|
218
|
|
|
* defined by the document's source order. |
|
219
|
|
|
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order |
|
220
|
|
|
* defined by the value of the number. That is, tabindex="4" is focused before tabindex="5", but after tabindex="3". |
|
221
|
|
|
* |
|
222
|
|
|
* @param int $value |
|
223
|
|
|
* |
|
224
|
|
|
* @return self |
|
225
|
|
|
*/ |
|
226
|
1 |
|
public function tabIndex(int $value = 0): self |
|
227
|
|
|
{ |
|
228
|
1 |
|
$new = clone $this; |
|
229
|
1 |
|
$new->options['tabindex'] = $value; |
|
230
|
1 |
|
return $new; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* The title global attribute contains text representing advisory information related to the element it belongs to. |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $value |
|
237
|
|
|
* |
|
238
|
|
|
* @return self |
|
239
|
|
|
*/ |
|
240
|
1 |
|
public function title(string $value): self |
|
241
|
|
|
{ |
|
242
|
1 |
|
$new = clone $this; |
|
243
|
1 |
|
$new->options['title'] = $value; |
|
244
|
1 |
|
return $new; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|