1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Form\Field\Base\InputField; |
9
|
|
|
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderInterface; |
10
|
|
|
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait; |
11
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; |
12
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait; |
13
|
|
|
use Yiisoft\Html\Html; |
14
|
|
|
|
15
|
|
|
use function is_string; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A one-line plain-text edit control for entering a password. |
19
|
|
|
* |
20
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#password-state-(type=password) |
21
|
|
|
*/ |
22
|
|
|
final class Password extends InputField implements PlaceholderInterface, ValidationClassInterface |
23
|
|
|
{ |
24
|
|
|
use PlaceholderTrait; |
25
|
|
|
use ValidationClassTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Maximum length of value. |
29
|
|
|
* |
30
|
|
|
* @param int $value A limit on the number of characters a user can input. |
31
|
|
|
* |
32
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength |
33
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength |
34
|
|
|
*/ |
35
|
|
|
public function maxlength(int $value): self |
36
|
|
|
{ |
37
|
|
|
$new = clone $this; |
38
|
|
|
$new->inputTagAttributes['maxlength'] = $value; |
39
|
|
|
return $new; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Minimum length of value. |
44
|
|
|
* |
45
|
|
|
* @param int $value A lower bound on the number of characters a user can input. |
46
|
|
|
* |
47
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength |
48
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength |
49
|
|
|
*/ |
50
|
|
|
public function minlength(int $value): self |
51
|
|
|
{ |
52
|
|
|
$new = clone $this; |
53
|
|
|
$new->inputTagAttributes['minlength'] = $value; |
54
|
|
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Pattern to be matched by the form control's value. |
59
|
|
|
* |
60
|
|
|
* @param string $value A regular expression against which the control's value. |
61
|
|
|
* |
62
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern |
63
|
|
|
*/ |
64
|
|
|
public function pattern(string $value): self |
65
|
|
|
{ |
66
|
|
|
$new = clone $this; |
67
|
|
|
$new->inputTagAttributes['pattern'] = $value; |
68
|
|
|
return $new; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* A boolean attribute that controls whether or not the user can edit the form control. |
73
|
|
|
* |
74
|
|
|
* @param bool $value Whether to allow the value to be edited by the user. |
75
|
|
|
* |
76
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly |
77
|
|
|
*/ |
78
|
|
|
public function readonly(bool $value = true): self |
79
|
|
|
{ |
80
|
|
|
$new = clone $this; |
81
|
|
|
$new->inputTagAttributes['readonly'] = $value; |
82
|
|
|
return $new; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* A boolean attribute. When specified, the element is required. |
87
|
|
|
* |
88
|
|
|
* @param bool $value Whether the control is required for form submission. |
89
|
|
|
* |
90
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required |
91
|
|
|
*/ |
92
|
|
|
public function required(bool $value = true): self |
93
|
|
|
{ |
94
|
|
|
$new = clone $this; |
95
|
|
|
$new->inputTagAttributes['required'] = $value; |
96
|
|
|
return $new; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled |
101
|
|
|
*/ |
102
|
|
|
public function disabled(bool $disabled = true): self |
103
|
|
|
{ |
104
|
|
|
$new = clone $this; |
105
|
|
|
$new->inputTagAttributes['disabled'] = $disabled; |
106
|
|
|
return $new; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Identifies the element (or elements) that describes the object. |
111
|
|
|
* |
112
|
|
|
* @link https://w3c.github.io/aria/#aria-describedby |
113
|
|
|
*/ |
114
|
|
|
public function ariaDescribedBy(string $value): self |
115
|
|
|
{ |
116
|
|
|
$new = clone $this; |
117
|
|
|
$new->inputTagAttributes['aria-describedby'] = $value; |
118
|
|
|
return $new; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Defines a string value that labels the current element. |
123
|
|
|
* |
124
|
|
|
* @link https://w3c.github.io/aria/#aria-label |
125
|
|
|
*/ |
126
|
|
|
public function ariaLabel(string $value): self |
127
|
|
|
{ |
128
|
|
|
$new = clone $this; |
129
|
|
|
$new->inputTagAttributes['aria-label'] = $value; |
130
|
|
|
return $new; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus |
135
|
|
|
* at the same time. |
136
|
|
|
* |
137
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus |
138
|
|
|
*/ |
139
|
|
|
public function autofocus(bool $value = true): self |
140
|
|
|
{ |
141
|
|
|
$new = clone $this; |
142
|
|
|
$new->inputTagAttributes['autofocus'] = $value; |
143
|
|
|
return $new; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential |
148
|
|
|
* keyboard navigation (usually with the Tab key, hence the name). |
149
|
|
|
* |
150
|
|
|
* It accepts an integer as a value, with different results depending on the integer's value: |
151
|
|
|
* |
152
|
|
|
* - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard |
153
|
|
|
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets |
154
|
|
|
* with JavaScript. |
155
|
|
|
* - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is |
156
|
|
|
* defined by the document's source order. |
157
|
|
|
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order |
158
|
|
|
* defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after |
159
|
|
|
* `tabindex="3"`. |
160
|
|
|
* |
161
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex |
162
|
|
|
*/ |
163
|
|
|
public function tabIndex(?int $value): self |
164
|
|
|
{ |
165
|
|
|
$new = clone $this; |
166
|
|
|
$new->inputTagAttributes['tabindex'] = $value; |
167
|
|
|
return $new; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* The size of the control. |
172
|
|
|
* |
173
|
|
|
* @param int $value The number of characters that allow the user to see while editing the element's value. |
174
|
|
|
* |
175
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size |
176
|
|
|
*/ |
177
|
|
|
public function size(int $value): self |
178
|
|
|
{ |
179
|
|
|
$new = clone $this; |
180
|
|
|
$new->inputTagAttributes['size'] = $value; |
181
|
|
|
return $new; |
182
|
|
|
} |
183
|
|
|
|
184
|
2 |
|
protected function generateInput(): string |
185
|
|
|
{ |
186
|
2 |
|
$value = $this->getAttributeValue(); |
187
|
|
|
|
188
|
2 |
|
if (!is_string($value) && $value !== null) { |
189
|
|
|
throw new InvalidArgumentException('Password widget must be a string or null value.'); |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
$tagAttributes = $this->getInputTagAttributes(); |
193
|
|
|
|
194
|
2 |
|
return Html::passwordInput($this->getInputName(), $value, $tagAttributes)->render(); |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
protected function prepareContainerTagAttributes(array &$attributes): void |
198
|
|
|
{ |
199
|
2 |
|
if ($this->hasFormModelAndAttribute()) { |
200
|
2 |
|
$this->addValidationClassToTagAttributes( |
201
|
|
|
$attributes, |
202
|
2 |
|
$this->getFormModel(), |
203
|
2 |
|
$this->getAttributeName(), |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
2 |
|
protected function prepareInputTagAttributes(array &$attributes): void |
209
|
|
|
{ |
210
|
2 |
|
$this->preparePlaceholderInInputTagAttributes($attributes); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|