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