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