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