|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Factory\Exception\InvalidConfigException; |
|
8
|
|
|
use Yiisoft\Form\FormModelInterface; |
|
9
|
|
|
use Yiisoft\Widget\Widget; |
|
10
|
|
|
|
|
11
|
|
|
final class DropDownList extends Widget |
|
12
|
|
|
{ |
|
13
|
|
|
private FormModelInterface $data; |
|
14
|
|
|
private string $attribute; |
|
15
|
|
|
private array $options = []; |
|
16
|
|
|
private array $items = []; |
|
17
|
|
|
private bool $noUnselect = false; |
|
18
|
|
|
private string $unselect = ''; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Generates a drop-down list for the given form attribute. |
|
22
|
|
|
* |
|
23
|
|
|
* The selection of the drop-down list is taken from the value of the form attribute. |
|
24
|
|
|
* |
|
25
|
|
|
* @throws InvalidConfigException |
|
26
|
|
|
* |
|
27
|
|
|
* @return string the generated drop-down list tag. |
|
28
|
|
|
*/ |
|
29
|
13 |
|
public function run(): string |
|
30
|
|
|
{ |
|
31
|
13 |
|
$multiple = $this->options['multiple'] ?? false; |
|
32
|
|
|
|
|
33
|
13 |
|
if (!$multiple) { |
|
34
|
9 |
|
return ListInput::widget() |
|
35
|
9 |
|
->type('dropDownList') |
|
|
|
|
|
|
36
|
9 |
|
->config($this->data, $this->attribute, $this->options) |
|
37
|
9 |
|
->items($this->items) |
|
38
|
9 |
|
->noUnselect($this->noUnselect) |
|
39
|
9 |
|
->unselect($this->unselect) |
|
40
|
9 |
|
->run(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
return ListBox::widget() |
|
44
|
4 |
|
->type('dropDownList') |
|
45
|
4 |
|
->config($this->data, $this->attribute, $this->options) |
|
46
|
4 |
|
->items($this->items) |
|
47
|
4 |
|
->noUnselect($this->noUnselect) |
|
48
|
4 |
|
->unselect($this->unselect) |
|
49
|
4 |
|
->run(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set form model, name and options for the widget. |
|
54
|
|
|
* |
|
55
|
|
|
* @param FormModelInterface $data Form model. |
|
56
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
|
57
|
|
|
* @param array $options The HTML attributes for the widget container tag. |
|
58
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
|
59
|
|
|
* |
|
60
|
|
|
* @return self |
|
61
|
|
|
*/ |
|
62
|
13 |
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
|
63
|
|
|
{ |
|
64
|
13 |
|
$new = clone $this; |
|
65
|
13 |
|
$new->data = $data; |
|
66
|
13 |
|
$new->attribute = $attribute; |
|
67
|
13 |
|
$new->options = $options; |
|
68
|
13 |
|
return $new; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Whether to HTML-encode the dropdownlist labels. |
|
73
|
|
|
* |
|
74
|
|
|
* Defaults to true. This option is ignored if item option is set. |
|
75
|
|
|
* |
|
76
|
|
|
* @param bool $value |
|
77
|
|
|
* |
|
78
|
|
|
* @return self |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function noEncode(bool $value = false): self |
|
81
|
|
|
{ |
|
82
|
1 |
|
$new = clone $this; |
|
83
|
1 |
|
$new->options['encode'] = $value; |
|
84
|
1 |
|
return $new; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* The attributes for the optgroup tags. |
|
89
|
|
|
* |
|
90
|
|
|
* The structure of this is similar to that of 'options', except that the array keys represent the optgroup labels |
|
91
|
|
|
* specified in $items. |
|
92
|
|
|
* |
|
93
|
|
|
* ```php |
|
94
|
|
|
* [ |
|
95
|
|
|
* 'groups' => [ |
|
96
|
|
|
* '1' => ['label' => 'Chile'], |
|
97
|
|
|
* '2' => ['label' => 'Russia'] |
|
98
|
|
|
* ], |
|
99
|
|
|
* ]; |
|
100
|
|
|
* ``` |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $value |
|
103
|
|
|
* |
|
104
|
|
|
* @return self |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function groups(array $value = []): self |
|
107
|
|
|
{ |
|
108
|
1 |
|
$new = clone $this; |
|
109
|
1 |
|
$new->options['groups'] = $value; |
|
110
|
1 |
|
return $new; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* The option data items. |
|
115
|
|
|
* |
|
116
|
|
|
* The array keys are option values, and the array values are the corresponding option labels. The array can also |
|
117
|
|
|
* be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose |
|
118
|
|
|
* label is the key associated with the sub-array. If you have a list of data {@see FormModel}, you may convert |
|
119
|
|
|
* them into the format described above using {@see \Yiisoft\Arrays\ArrayHelper::map()} |
|
120
|
|
|
* |
|
121
|
|
|
* Example: |
|
122
|
|
|
* ```php |
|
123
|
|
|
* [ |
|
124
|
|
|
* '1' => 'Santiago', |
|
125
|
|
|
* '2' => 'Concepcion', |
|
126
|
|
|
* '3' => 'Chillan', |
|
127
|
|
|
* '4' => 'Moscu' |
|
128
|
|
|
* '5' => 'San Petersburgo', |
|
129
|
|
|
* '6' => 'Novosibirsk', |
|
130
|
|
|
* '7' => 'Ekaterinburgo' |
|
131
|
|
|
* ]; |
|
132
|
|
|
* ``` |
|
133
|
|
|
* |
|
134
|
|
|
* Example with options groups: |
|
135
|
|
|
* ```php |
|
136
|
|
|
* [ |
|
137
|
|
|
* '1' => [ |
|
138
|
|
|
* '1' => 'Santiago', |
|
139
|
|
|
* '2' => 'Concepcion', |
|
140
|
|
|
* '3' => 'Chillan', |
|
141
|
|
|
* ], |
|
142
|
|
|
* '2' => [ |
|
143
|
|
|
* '4' => 'Moscu', |
|
144
|
|
|
* '5' => 'San Petersburgo', |
|
145
|
|
|
* '6' => 'Novosibirsk', |
|
146
|
|
|
* '7' => 'Ekaterinburgo' |
|
147
|
|
|
* ], |
|
148
|
|
|
* ]; |
|
149
|
|
|
* ``` |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $value |
|
152
|
|
|
* |
|
153
|
|
|
* @return self |
|
154
|
|
|
*/ |
|
155
|
13 |
|
public function items(array $value = []): self |
|
156
|
|
|
{ |
|
157
|
13 |
|
$new = clone $this; |
|
158
|
13 |
|
$new->items = $value; |
|
159
|
13 |
|
return $new; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* The Boolean multiple attribute, if set, means the widget accepts one or more values. |
|
164
|
|
|
* |
|
165
|
|
|
* Most browsers displaying a scrolling list box for a <select> control with the multiple attribute set versus a |
|
166
|
|
|
* single line dropdown when the attribute is false. |
|
167
|
|
|
* |
|
168
|
|
|
* @param bool $value |
|
169
|
|
|
* |
|
170
|
|
|
* @return self |
|
171
|
|
|
*/ |
|
172
|
3 |
|
public function multiple(bool $value = true): self |
|
173
|
|
|
{ |
|
174
|
3 |
|
$new = clone $this; |
|
175
|
3 |
|
$new->options['multiple'] = $value; |
|
176
|
3 |
|
return $new; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Prompt text to be displayed as the first option, you can use an array to override the value and to set other |
|
181
|
|
|
* tag attributes: |
|
182
|
|
|
* |
|
183
|
|
|
* ```php |
|
184
|
|
|
* [ |
|
185
|
|
|
* 'prompt' => [ |
|
186
|
|
|
* 'text' => 'Select City Birth', |
|
187
|
|
|
* 'options' => [ |
|
188
|
|
|
* 'value' => '0', |
|
189
|
|
|
* 'selected' => 'selected' |
|
190
|
|
|
* ], |
|
191
|
|
|
* ], |
|
192
|
|
|
* ] |
|
193
|
|
|
* ``` |
|
194
|
|
|
* |
|
195
|
|
|
* @param array $value |
|
196
|
|
|
* |
|
197
|
|
|
* @return self |
|
198
|
|
|
*/ |
|
199
|
1 |
|
public function prompt(array $value = []): self |
|
200
|
|
|
{ |
|
201
|
1 |
|
$new = clone $this; |
|
202
|
1 |
|
$new->options['prompt'] = $value; |
|
203
|
1 |
|
return $new; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* If it is required to fill in a value in order to submit the form. |
|
208
|
|
|
* |
|
209
|
|
|
* @param bool $value |
|
210
|
|
|
* |
|
211
|
|
|
* @return self |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function required(bool $value = true): self |
|
214
|
|
|
{ |
|
215
|
1 |
|
$new = clone $this; |
|
216
|
1 |
|
$new->options['required'] = $value; |
|
217
|
1 |
|
return $new; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* The height of the <select> with multiple is true. |
|
222
|
|
|
* |
|
223
|
|
|
* Default value is 4. |
|
224
|
|
|
* |
|
225
|
|
|
* @param int $value |
|
226
|
|
|
* |
|
227
|
|
|
* @return self |
|
228
|
|
|
*/ |
|
229
|
1 |
|
public function size(int $value = 4): self |
|
230
|
|
|
{ |
|
231
|
1 |
|
$new = clone $this; |
|
232
|
1 |
|
$new->options['size'] = $value; |
|
233
|
1 |
|
return $new; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* The value that should be submitted when none of the dropdown list is selected. |
|
238
|
|
|
* |
|
239
|
|
|
* You may set this option to be null to prevent default value submission. If this option is not set, an empty |
|
240
|
|
|
* string will be submitted. |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $value |
|
243
|
|
|
* |
|
244
|
|
|
* @return self |
|
245
|
|
|
*/ |
|
246
|
1 |
|
public function unselect(string $value): self |
|
247
|
|
|
{ |
|
248
|
1 |
|
$new = clone $this; |
|
249
|
1 |
|
$new->unselect = $value; |
|
250
|
1 |
|
return $new; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|