|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesInterface; |
|
9
|
|
|
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesTrait; |
|
10
|
|
|
use Yiisoft\Form\Field\Base\InputField; |
|
11
|
|
|
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderInterface; |
|
12
|
|
|
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait; |
|
13
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; |
|
14
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait; |
|
15
|
|
|
use Yiisoft\Html\Html; |
|
16
|
|
|
use Yiisoft\Validator\Rule\Length; |
|
17
|
|
|
use Yiisoft\Validator\Rule\Regex; |
|
18
|
|
|
use Yiisoft\Validator\Rule\Required; |
|
19
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
20
|
|
|
|
|
21
|
|
|
use function is_string; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email) |
|
25
|
|
|
* @link https://developer.mozilla.org/docs/Web/HTML/Element/input/email |
|
26
|
|
|
*/ |
|
27
|
|
|
final class Email extends InputField implements EnrichmentFromRulesInterface, PlaceholderInterface, ValidationClassInterface |
|
28
|
|
|
{ |
|
29
|
|
|
use EnrichmentFromRulesTrait; |
|
30
|
|
|
use PlaceholderTrait; |
|
31
|
|
|
use ValidationClassTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Maximum length of value. |
|
35
|
|
|
* |
|
36
|
|
|
* @param int|null $value A limit on the number of characters a user can input. |
|
37
|
|
|
* |
|
38
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength |
|
39
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function maxlength(?int $value): self |
|
42
|
|
|
{ |
|
43
|
2 |
|
$new = clone $this; |
|
44
|
2 |
|
$new->inputAttributes['maxlength'] = $value; |
|
45
|
2 |
|
return $new; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Minimum length of value. |
|
50
|
|
|
* |
|
51
|
|
|
* @param int|null $value A lower bound on the number of characters a user can input. |
|
52
|
|
|
* |
|
53
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength |
|
54
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function minlength(?int $value): self |
|
57
|
|
|
{ |
|
58
|
2 |
|
$new = clone $this; |
|
59
|
2 |
|
$new->inputAttributes['minlength'] = $value; |
|
60
|
2 |
|
return $new; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Allow to specify more than one value. |
|
65
|
|
|
* |
|
66
|
|
|
* @param bool $value Whether the user is to be allowed to specify more than one value. |
|
67
|
|
|
* |
|
68
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-multiple |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function multiple(bool $value = true): self |
|
71
|
|
|
{ |
|
72
|
2 |
|
$new = clone $this; |
|
73
|
2 |
|
$new->inputAttributes['multiple'] = $value; |
|
74
|
2 |
|
return $new; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Pattern to be matched by the form control's value. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string|null $value A regular expression against which the control's value. |
|
81
|
|
|
* |
|
82
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function pattern(?string $value): self |
|
85
|
|
|
{ |
|
86
|
2 |
|
$new = clone $this; |
|
87
|
2 |
|
$new->inputAttributes['pattern'] = $value; |
|
88
|
2 |
|
return $new; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* A boolean attribute that controls whether or not the user can edit the form control. |
|
93
|
|
|
* |
|
94
|
|
|
* @param bool $value Whether to allow the value to be edited by the user. |
|
95
|
|
|
* |
|
96
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function readonly(bool $value = true): self |
|
99
|
|
|
{ |
|
100
|
2 |
|
$new = clone $this; |
|
101
|
2 |
|
$new->inputAttributes['readonly'] = $value; |
|
102
|
2 |
|
return $new; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* A boolean attribute. When specified, the element is required. |
|
107
|
|
|
* |
|
108
|
|
|
* @param bool $value Whether the control is required for form submission. |
|
109
|
|
|
* |
|
110
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function required(bool $value = true): self |
|
113
|
|
|
{ |
|
114
|
2 |
|
$new = clone $this; |
|
115
|
2 |
|
$new->inputAttributes['required'] = $value; |
|
116
|
2 |
|
return $new; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* The size of the control. |
|
121
|
|
|
* |
|
122
|
|
|
* @param int|null $value The number of characters that allow the user to see while editing the element's value. |
|
123
|
|
|
* |
|
124
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size |
|
125
|
|
|
*/ |
|
126
|
2 |
|
public function size(?int $value): self |
|
127
|
|
|
{ |
|
128
|
2 |
|
$new = clone $this; |
|
129
|
2 |
|
$new->inputAttributes['size'] = $value; |
|
130
|
2 |
|
return $new; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled |
|
135
|
|
|
*/ |
|
136
|
2 |
|
public function disabled(bool $disabled = true): self |
|
137
|
|
|
{ |
|
138
|
2 |
|
$new = clone $this; |
|
139
|
2 |
|
$new->inputAttributes['disabled'] = $disabled; |
|
140
|
2 |
|
return $new; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Identifies the element (or elements) that describes the object. |
|
145
|
|
|
* |
|
146
|
|
|
* @link https://w3c.github.io/aria/#aria-describedby |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function ariaDescribedBy(?string $value): self |
|
149
|
|
|
{ |
|
150
|
2 |
|
$new = clone $this; |
|
151
|
2 |
|
$new->inputAttributes['aria-describedby'] = $value; |
|
152
|
2 |
|
return $new; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Defines a string value that labels the current element. |
|
157
|
|
|
* |
|
158
|
|
|
* @link https://w3c.github.io/aria/#aria-label |
|
159
|
|
|
*/ |
|
160
|
2 |
|
public function ariaLabel(?string $value): self |
|
161
|
|
|
{ |
|
162
|
2 |
|
$new = clone $this; |
|
163
|
2 |
|
$new->inputAttributes['aria-label'] = $value; |
|
164
|
2 |
|
return $new; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus |
|
169
|
|
|
* at the same time. |
|
170
|
|
|
* |
|
171
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus |
|
172
|
|
|
*/ |
|
173
|
2 |
|
public function autofocus(bool $value = true): self |
|
174
|
|
|
{ |
|
175
|
2 |
|
$new = clone $this; |
|
176
|
2 |
|
$new->inputAttributes['autofocus'] = $value; |
|
177
|
2 |
|
return $new; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential |
|
182
|
|
|
* keyboard navigation (usually with the Tab key, hence the name). |
|
183
|
|
|
* |
|
184
|
|
|
* It accepts an integer as a value, with different results depending on the integer's value: |
|
185
|
|
|
* |
|
186
|
|
|
* - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard |
|
187
|
|
|
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets |
|
188
|
|
|
* with JavaScript. |
|
189
|
|
|
* - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is |
|
190
|
|
|
* defined by the document's source order. |
|
191
|
|
|
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order |
|
192
|
|
|
* defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after |
|
193
|
|
|
* `tabindex="3"`. |
|
194
|
|
|
* |
|
195
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex |
|
196
|
|
|
*/ |
|
197
|
2 |
|
public function tabIndex(?int $value): self |
|
198
|
|
|
{ |
|
199
|
2 |
|
$new = clone $this; |
|
200
|
2 |
|
$new->inputAttributes['tabindex'] = $value; |
|
201
|
2 |
|
return $new; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @psalm-suppress MixedAssignment,MixedArgument |
|
206
|
|
|
*/ |
|
207
|
20 |
|
protected function beforeRender(): void |
|
208
|
|
|
{ |
|
209
|
20 |
|
parent::beforeRender(); |
|
210
|
20 |
|
if ($this->enrichmentFromRules) { |
|
211
|
5 |
|
foreach ($this->getInputData()->getValidationRules() as $rule) { |
|
212
|
5 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
|
213
|
1 |
|
continue; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
4 |
|
if ($rule instanceof Required) { |
|
217
|
1 |
|
$this->inputAttributes['required'] = true; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
4 |
|
if ($rule instanceof Length) { |
|
221
|
1 |
|
if (null !== $min = $rule->getMin()) { |
|
222
|
1 |
|
$this->inputAttributes['minlength'] = $min; |
|
223
|
|
|
} |
|
224
|
1 |
|
if (null !== $max = $rule->getMax()) { |
|
225
|
1 |
|
$this->inputAttributes['maxlength'] = $max; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
4 |
|
if ($rule instanceof Regex) { |
|
230
|
2 |
|
if (!$rule->isNot()) { |
|
231
|
1 |
|
$this->inputAttributes['pattern'] = Html::normalizeRegexpPattern( |
|
232
|
1 |
|
$rule->getPattern() |
|
233
|
1 |
|
); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
20 |
|
protected function generateInput(): string |
|
241
|
|
|
{ |
|
242
|
20 |
|
$value = $this->getInputData()->getValue(); |
|
243
|
|
|
|
|
244
|
20 |
|
if (!is_string($value) && $value !== null) { |
|
245
|
1 |
|
throw new InvalidArgumentException('Email field requires a string or null value.'); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
19 |
|
$attributes = $this->getInputAttributes(); |
|
249
|
|
|
|
|
250
|
19 |
|
return Html::input('email', $this->getInputData()->getName(), $value, $attributes)->render(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
14 |
|
protected function prepareContainerAttributes(array &$attributes): void |
|
254
|
|
|
{ |
|
255
|
14 |
|
$this->addValidationClassToAttributes($attributes, $this->getInputData()); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
19 |
|
protected function prepareInputAttributes(array &$attributes): void |
|
259
|
|
|
{ |
|
260
|
19 |
|
$this->preparePlaceholderInInputAttributes($attributes); |
|
261
|
19 |
|
$this->addInputValidationClassToAttributes($attributes, $this->getInputData()); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|