|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
|
6
|
|
|
|
|
7
|
|
|
use Stringable; |
|
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\ValidationClass\ValidationClassInterface; |
|
12
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait; |
|
13
|
|
|
use Yiisoft\Html\Html; |
|
14
|
|
|
use Yiisoft\Validator\Rule\Required; |
|
15
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Represents `<input>` element of type "file" are ley the user choose one or more files from their device storage. |
|
19
|
|
|
* |
|
20
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#file-upload-state-(type=file) |
|
21
|
|
|
* @link https://developer.mozilla.org/docs/Web/HTML/Element/input/file |
|
22
|
|
|
*/ |
|
23
|
|
|
final class File extends InputField implements EnrichmentFromRulesInterface, ValidationClassInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use EnrichmentFromRulesTrait; |
|
26
|
|
|
use ValidationClassTrait; |
|
27
|
|
|
|
|
28
|
|
|
private bool|float|int|string|Stringable|null $uncheckValue = null; |
|
29
|
|
|
private array $uncheckInputAttributes = []; |
|
30
|
|
|
private string|Stringable|null $value = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The accept attribute value is a string that defines the file types the file input should accept. This string is |
|
34
|
|
|
* a comma-separated list of unique file type specifiers. Because a given file type may be identified in more than |
|
35
|
|
|
* one manner, it's useful to provide a thorough set of type specifiers when you need files of a given format. |
|
36
|
|
|
* |
|
37
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-accept |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function accept(?string $value): self |
|
40
|
|
|
{ |
|
41
|
2 |
|
$new = clone $this; |
|
42
|
2 |
|
$new->inputAttributes['accept'] = $value; |
|
43
|
2 |
|
return $new; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param bool $multiple Whether to allow selecting multiple files. |
|
48
|
|
|
* |
|
49
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-multiple |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function multiple(bool $multiple = true): self |
|
52
|
|
|
{ |
|
53
|
2 |
|
$new = clone $this; |
|
54
|
2 |
|
$new->inputAttributes['multiple'] = $multiple; |
|
55
|
2 |
|
return $new; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* A boolean attribute. When specified, the element is required. |
|
60
|
|
|
* |
|
61
|
|
|
* @param bool $value Whether the control is required for form submission. |
|
62
|
|
|
* |
|
63
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function required(bool $value = true): self |
|
66
|
|
|
{ |
|
67
|
2 |
|
$new = clone $this; |
|
68
|
2 |
|
$new->inputAttributes['required'] = $value; |
|
69
|
2 |
|
return $new; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param bool $disabled Whether select input is disabled. |
|
74
|
|
|
* |
|
75
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function disabled(bool $disabled = true): self |
|
78
|
|
|
{ |
|
79
|
3 |
|
$new = clone $this; |
|
80
|
3 |
|
$new->inputAttributes['disabled'] = $disabled; |
|
81
|
3 |
|
return $new; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Identifies the element (or elements) that describes the object. |
|
86
|
|
|
* |
|
87
|
|
|
* @link https://w3c.github.io/aria/#aria-describedby |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function ariaDescribedBy(?string $value): self |
|
90
|
|
|
{ |
|
91
|
2 |
|
$new = clone $this; |
|
92
|
2 |
|
$new->inputAttributes['aria-describedby'] = $value; |
|
93
|
2 |
|
return $new; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Defines a string value that labels the current element. |
|
98
|
|
|
* |
|
99
|
|
|
* @link https://w3c.github.io/aria/#aria-label |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function ariaLabel(?string $value): self |
|
102
|
|
|
{ |
|
103
|
2 |
|
$new = clone $this; |
|
104
|
2 |
|
$new->inputAttributes['aria-label'] = $value; |
|
105
|
2 |
|
return $new; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential |
|
110
|
|
|
* keyboard navigation (usually with the Tab key, hence the name). |
|
111
|
|
|
* |
|
112
|
|
|
* It accepts an integer as a value, with different results depending on the integer's value: |
|
113
|
|
|
* |
|
114
|
|
|
* - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard |
|
115
|
|
|
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets |
|
116
|
|
|
* with JavaScript. |
|
117
|
|
|
* - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is |
|
118
|
|
|
* defined by the document's source order. |
|
119
|
|
|
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order |
|
120
|
|
|
* defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after |
|
121
|
|
|
* `tabindex="3"`. |
|
122
|
|
|
* |
|
123
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public function tabIndex(?int $value): self |
|
126
|
|
|
{ |
|
127
|
2 |
|
$new = clone $this; |
|
128
|
2 |
|
$new->inputAttributes['tabindex'] = $value; |
|
129
|
2 |
|
return $new; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param bool|float|int|string|Stringable|null $value Value that corresponds to "unchecked" state of the input. |
|
134
|
|
|
*/ |
|
135
|
6 |
|
public function uncheckValue(bool|float|int|string|Stringable|null $value): self |
|
136
|
|
|
{ |
|
137
|
6 |
|
$new = clone $this; |
|
138
|
6 |
|
$new->uncheckValue = $value; |
|
139
|
6 |
|
return $new; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
public function uncheckInputAttributes(array $attributes): self |
|
143
|
|
|
{ |
|
144
|
2 |
|
$new = clone $this; |
|
145
|
2 |
|
$new->uncheckInputAttributes = $attributes; |
|
146
|
2 |
|
return $new; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
public function addUncheckInputAttributes(array $attributes): self |
|
150
|
|
|
{ |
|
151
|
2 |
|
$new = clone $this; |
|
152
|
2 |
|
$new->uncheckInputAttributes = array_merge($new->uncheckInputAttributes, $attributes); |
|
153
|
2 |
|
return $new; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
2 |
|
public function value(string|Stringable|null $value): self |
|
157
|
|
|
{ |
|
158
|
2 |
|
$new = clone $this; |
|
159
|
2 |
|
$new->value = $value; |
|
160
|
2 |
|
return $new; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @psalm-suppress MixedAssignment,MixedArgument |
|
165
|
|
|
*/ |
|
166
|
17 |
|
protected function beforeRender(): void |
|
167
|
|
|
{ |
|
168
|
17 |
|
parent::beforeRender(); |
|
169
|
17 |
|
if ($this->enrichmentFromRules) { |
|
170
|
2 |
|
foreach ($this->getInputData()->getValidationRules() as $rule) { |
|
171
|
2 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
|
172
|
1 |
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
1 |
|
if ($rule instanceof Required) { |
|
176
|
1 |
|
$this->inputAttributes['required'] = true; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
17 |
|
protected function generateInput(): string |
|
183
|
|
|
{ |
|
184
|
17 |
|
$inputAttributes = $this->getInputAttributes(); |
|
185
|
|
|
|
|
186
|
17 |
|
$tag = Html::file($this->getInputData()->getName(), $this->value, $inputAttributes); |
|
187
|
17 |
|
if ($this->uncheckValue !== null) { |
|
188
|
5 |
|
$tag = $tag->uncheckValue($this->uncheckValue); |
|
189
|
5 |
|
if (!empty($this->uncheckInputAttributes)) { |
|
190
|
2 |
|
$tag = $tag->addUncheckInputAttributes($this->uncheckInputAttributes); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
17 |
|
return $tag->render(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
10 |
|
protected function prepareContainerAttributes(array &$attributes): void |
|
198
|
|
|
{ |
|
199
|
10 |
|
$this->addValidationClassToAttributes($attributes, $this->getInputData()); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
17 |
|
protected function prepareInputAttributes(array &$attributes): void |
|
203
|
|
|
{ |
|
204
|
17 |
|
$this->addInputValidationClassToAttributes($attributes, $this->getInputData()); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|