1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Stringable; |
9
|
|
|
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesInterface; |
10
|
|
|
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesTrait; |
11
|
|
|
use Yiisoft\Form\Field\Base\InputField; |
12
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; |
13
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait; |
14
|
|
|
use Yiisoft\Html\Tag\Optgroup; |
15
|
|
|
use Yiisoft\Html\Tag\Option; |
16
|
|
|
use Yiisoft\Html\Tag\Select as SelectTag; |
17
|
|
|
use Yiisoft\Validator\BeforeValidationInterface; |
18
|
|
|
use Yiisoft\Validator\Rule\Required; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represents `<select>` element that provides a menu of options. |
22
|
|
|
* |
23
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element |
24
|
|
|
* @link https://developer.mozilla.org/docs/Web/HTML/Element/select |
25
|
|
|
*/ |
26
|
|
|
final class Select extends InputField implements EnrichmentFromRulesInterface, ValidationClassInterface |
27
|
|
|
{ |
28
|
|
|
use EnrichmentFromRulesTrait; |
29
|
|
|
use ValidationClassTrait; |
30
|
|
|
|
31
|
|
|
private SelectTag $select; |
32
|
|
|
|
33
|
31 |
|
public function __construct() |
34
|
|
|
{ |
35
|
31 |
|
$this->select = SelectTag::tag(); |
36
|
|
|
} |
37
|
|
|
|
38
|
5 |
|
public function items(Optgroup|Option ...$items): self |
39
|
|
|
{ |
40
|
5 |
|
$new = clone $this; |
41
|
5 |
|
$new->select = $this->select->items(...$items); |
42
|
5 |
|
return $new; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function options(Option ...$options): self |
46
|
|
|
{ |
47
|
2 |
|
return $this->items(...$options); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $data Options data. The array keys are option values, and the array values are the corresponding |
52
|
|
|
* option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, |
53
|
|
|
* an option group will be generated whose label is the key associated with the sub-array. |
54
|
|
|
* |
55
|
|
|
* Example: |
56
|
|
|
* ```php |
57
|
|
|
* [ |
58
|
|
|
* '1' => 'Santiago', |
59
|
|
|
* '2' => 'Concepcion', |
60
|
|
|
* '3' => 'Chillan', |
61
|
|
|
* '4' => 'Moscow' |
62
|
|
|
* '5' => 'San Petersburg', |
63
|
|
|
* '6' => 'Novosibirsk', |
64
|
|
|
* '7' => 'Ekaterinburg' |
65
|
|
|
* ]; |
66
|
|
|
* ``` |
67
|
|
|
* |
68
|
|
|
* Example with options groups: |
69
|
|
|
* ```php |
70
|
|
|
* [ |
71
|
|
|
* '1' => [ |
72
|
|
|
* '1' => 'Santiago', |
73
|
|
|
* '2' => 'Concepcion', |
74
|
|
|
* '3' => 'Chillan', |
75
|
|
|
* ], |
76
|
|
|
* '2' => [ |
77
|
|
|
* '4' => 'Moscow', |
78
|
|
|
* '5' => 'San Petersburg', |
79
|
|
|
* '6' => 'Novosibirsk', |
80
|
|
|
* '7' => 'Ekaterinburg' |
81
|
|
|
* ], |
82
|
|
|
* ]; |
83
|
|
|
* ``` |
84
|
|
|
* @param bool $encode Whether option content should be HTML-encoded. |
85
|
|
|
* @param array[] $optionsAttributes Array of option attribute sets indexed by option values from {@see $data}. |
86
|
|
|
* @param array[] $groupsAttributes Array of group attribute sets indexed by group labels from {@see $data}. |
87
|
|
|
* |
88
|
|
|
* @psalm-param array<array-key, string|array<array-key,string>> $data |
89
|
|
|
* |
90
|
|
|
* @return self |
91
|
|
|
*/ |
92
|
25 |
|
public function optionsData( |
93
|
|
|
array $data, |
94
|
|
|
bool $encode = true, |
95
|
|
|
array $optionsAttributes = [], |
96
|
|
|
array $groupsAttributes = [] |
97
|
|
|
): self { |
98
|
25 |
|
$new = clone $this; |
99
|
25 |
|
$new->select = $this->select->optionsData($data, $encode, $optionsAttributes, $groupsAttributes); |
100
|
25 |
|
return $new; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param bool $disabled Whether select input is disabled. |
105
|
|
|
* |
106
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled |
107
|
|
|
*/ |
108
|
2 |
|
public function disabled(bool $disabled = true): self |
109
|
|
|
{ |
110
|
2 |
|
$new = clone $this; |
111
|
2 |
|
$new->inputAttributes['disabled'] = $disabled; |
112
|
2 |
|
return $new; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Identifies the element (or elements) that describes the object. |
117
|
|
|
* |
118
|
|
|
* @link https://w3c.github.io/aria/#aria-describedby |
119
|
|
|
*/ |
120
|
2 |
|
public function ariaDescribedBy(?string $value): self |
121
|
|
|
{ |
122
|
2 |
|
$new = clone $this; |
123
|
2 |
|
$new->inputAttributes['aria-describedby'] = $value; |
124
|
2 |
|
return $new; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Defines a string value that labels the current element. |
129
|
|
|
* |
130
|
|
|
* @link https://w3c.github.io/aria/#aria-label |
131
|
|
|
*/ |
132
|
2 |
|
public function ariaLabel(?string $value): self |
133
|
|
|
{ |
134
|
2 |
|
$new = clone $this; |
135
|
2 |
|
$new->inputAttributes['aria-label'] = $value; |
136
|
2 |
|
return $new; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus |
141
|
|
|
* at the same time. |
142
|
|
|
* |
143
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus |
144
|
|
|
*/ |
145
|
2 |
|
public function autofocus(bool $value = true): self |
146
|
|
|
{ |
147
|
2 |
|
$new = clone $this; |
148
|
2 |
|
$new->inputAttributes['autofocus'] = $value; |
149
|
2 |
|
return $new; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential |
154
|
|
|
* keyboard navigation (usually with the Tab key, hence the name). |
155
|
|
|
* |
156
|
|
|
* It accepts an integer as a value, with different results depending on the integer's value: |
157
|
|
|
* |
158
|
|
|
* - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard |
159
|
|
|
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets |
160
|
|
|
* with JavaScript. |
161
|
|
|
* - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is |
162
|
|
|
* defined by the document's source order. |
163
|
|
|
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order |
164
|
|
|
* defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after |
165
|
|
|
* `tabindex="3"`. |
166
|
|
|
* |
167
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex |
168
|
|
|
*/ |
169
|
2 |
|
public function tabIndex(?int $value): self |
170
|
|
|
{ |
171
|
2 |
|
$new = clone $this; |
172
|
2 |
|
$new->inputAttributes['tabindex'] = $value; |
173
|
2 |
|
return $new; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param bool $value Whether the user is to be allowed to select zero or more options. |
178
|
|
|
* |
179
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-select-multiple |
180
|
|
|
*/ |
181
|
4 |
|
public function multiple(bool $value = true): self |
182
|
|
|
{ |
183
|
4 |
|
$new = clone $this; |
184
|
4 |
|
$new->inputAttributes['multiple'] = $value; |
185
|
4 |
|
return $new; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string|null $text Text of the option that has dummy value and is rendered as an invitation to select |
190
|
|
|
* a value. |
191
|
|
|
*/ |
192
|
3 |
|
public function prompt(?string $text): self |
193
|
|
|
{ |
194
|
3 |
|
$new = clone $this; |
195
|
3 |
|
$new->select = $this->select->prompt($text); |
196
|
3 |
|
return $new; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param Option|null $option Option that has dummy value and is rendered as an invitation to select a value. |
201
|
|
|
*/ |
202
|
3 |
|
public function promptOption(?Option $option): self |
203
|
|
|
{ |
204
|
3 |
|
$new = clone $this; |
205
|
3 |
|
$new->select = $this->select->promptOption($option); |
206
|
3 |
|
return $new; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* A boolean attribute. When specified, the element is required. |
211
|
|
|
* |
212
|
|
|
* @param bool $value Whether the control is required for form submission. |
213
|
|
|
* |
214
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-select-required |
215
|
|
|
*/ |
216
|
2 |
|
public function required(bool $value = true): self |
217
|
|
|
{ |
218
|
2 |
|
$new = clone $this; |
219
|
2 |
|
$new->inputAttributes['required'] = $value; |
220
|
2 |
|
return $new; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* The size of the control. |
225
|
|
|
* |
226
|
|
|
* @param int|null $value The number of options to show to the user. |
227
|
|
|
* |
228
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-select-size |
229
|
|
|
*/ |
230
|
2 |
|
public function size(?int $value): self |
231
|
|
|
{ |
232
|
2 |
|
$new = clone $this; |
233
|
2 |
|
$new->inputAttributes['size'] = $value; |
234
|
2 |
|
return $new; |
235
|
|
|
} |
236
|
|
|
|
237
|
2 |
|
public function unselectValue(bool|float|int|string|Stringable|null $value): self |
238
|
|
|
{ |
239
|
2 |
|
$new = clone $this; |
240
|
2 |
|
$new->select = $this->select->unselectValue($value); |
241
|
2 |
|
return $new; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @psalm-suppress MixedAssignment,MixedArgument Remove after fix https://github.com/yiisoft/validator/issues/225 |
246
|
|
|
*/ |
247
|
30 |
|
protected function beforeRender(): void |
248
|
|
|
{ |
249
|
30 |
|
parent::beforeRender(); |
250
|
30 |
|
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) { |
251
|
2 |
|
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? []; |
252
|
2 |
|
foreach ($rules as $rule) { |
253
|
2 |
|
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) { |
254
|
1 |
|
continue; |
255
|
|
|
} |
256
|
|
|
|
257
|
1 |
|
if ($rule instanceof Required) { |
258
|
1 |
|
$this->inputAttributes['required'] = true; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
30 |
|
protected function generateInput(): string |
265
|
|
|
{ |
266
|
30 |
|
$value = $this->getFormAttributeValue(); |
267
|
30 |
|
$multiple = (bool) ($this->inputAttributes['multiple'] ?? false); |
268
|
|
|
|
269
|
30 |
|
if ($multiple) { |
270
|
|
|
/** @var mixed $value */ |
271
|
3 |
|
$value ??= []; |
272
|
3 |
|
if (!is_iterable($value)) { |
273
|
1 |
|
throw new InvalidArgumentException( |
274
|
|
|
'Select field with multiple option requires iterable or null value.' |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
} else { |
278
|
27 |
|
if (!is_bool($value) |
279
|
27 |
|
&& !is_string($value) |
280
|
27 |
|
&& !is_numeric($value) |
281
|
27 |
|
&& $value !== null |
282
|
27 |
|
&& (!is_object($value) || !method_exists($value, '__toString')) |
283
|
|
|
) { |
284
|
1 |
|
throw new InvalidArgumentException( |
285
|
|
|
'Non-multiple Select field requires a string, numeric, bool, Stringable or null value.' |
286
|
|
|
); |
287
|
|
|
} |
288
|
26 |
|
$value = $value === null ? [] : [$value]; |
289
|
|
|
} |
290
|
|
|
/** @psalm-var iterable<int, Stringable|scalar> $value */ |
291
|
|
|
|
292
|
28 |
|
$selectAttributes = $this->getInputAttributes(); |
293
|
|
|
|
294
|
28 |
|
return $this->select |
295
|
28 |
|
->attributes($selectAttributes) |
296
|
28 |
|
->name($this->getInputName()) |
297
|
28 |
|
->values($value) |
298
|
28 |
|
->render(); |
299
|
|
|
} |
300
|
|
|
|
301
|
4 |
|
protected function prepareContainerAttributes(array &$attributes): void |
302
|
|
|
{ |
303
|
4 |
|
if ($this->hasFormModelAndAttribute()) { |
304
|
4 |
|
$this->addValidationClassToAttributes( |
305
|
|
|
$attributes, |
306
|
4 |
|
$this->getFormModel(), |
307
|
4 |
|
$this->getFormAttributeName(), |
308
|
|
|
); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
28 |
|
protected function prepareInputAttributes(array &$attributes): void |
313
|
|
|
{ |
314
|
28 |
|
if ($this->hasFormModelAndAttribute()) { |
315
|
28 |
|
$this->addInputValidationClassToAttributes( |
316
|
|
|
$attributes, |
317
|
28 |
|
$this->getFormModel(), |
318
|
28 |
|
$this->getFormAttributeName(), |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths