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