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