1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\FormModelInterface; |
8
|
|
|
use Yiisoft\Widget\Widget; |
9
|
|
|
|
10
|
|
|
final class ListBox extends Widget |
11
|
|
|
{ |
12
|
|
|
private FormModelInterface $data; |
13
|
|
|
private string $attribute; |
14
|
|
|
private array $options = []; |
15
|
|
|
private array $items = []; |
16
|
|
|
private bool $noUnselect = false; |
17
|
|
|
private string $unselect = ''; |
18
|
|
|
private string $type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generates a list box. |
22
|
|
|
* |
23
|
|
|
* The selection of the list box is taken from the value of the model attribute. |
24
|
|
|
* |
25
|
|
|
* @return string the generated list box tag. |
26
|
|
|
*/ |
27
|
25 |
|
public function run(): string |
28
|
|
|
{ |
29
|
25 |
|
return ListInput::Widget() |
30
|
25 |
|
->type('listBox') |
|
|
|
|
31
|
25 |
|
->config($this->data, $this->attribute, $this->options) |
32
|
25 |
|
->items($this->items) |
33
|
25 |
|
->noUnselect($this->noUnselect) |
34
|
25 |
|
->unselect($this->unselect) |
35
|
25 |
|
->run(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set form model, name and options for the widget. |
40
|
|
|
* |
41
|
|
|
* @param FormModelInterface $data Form model. |
42
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
43
|
|
|
* @param array $options The HTML attributes for the widget container tag. |
44
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
25 |
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
49
|
|
|
{ |
50
|
25 |
|
$new = clone $this; |
51
|
25 |
|
$new->data = $data; |
52
|
25 |
|
$new->attribute = $attribute; |
53
|
25 |
|
$new->options = $options; |
54
|
25 |
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Whether to HTML-encode the listbox labels. |
59
|
|
|
* |
60
|
|
|
* Defaults to true. This option is ignored if item option is set. |
61
|
|
|
* |
62
|
|
|
* @param bool $value |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
1 |
|
public function noEncode(bool $value = false): self |
67
|
|
|
{ |
68
|
1 |
|
$new = clone $this; |
69
|
1 |
|
$new->options['encode'] = $value; |
70
|
1 |
|
return $new; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The attributes for the optgroup tags. |
75
|
|
|
* |
76
|
|
|
* The structure of this is similar to that of 'options', except that the array keys represent the optgroup labels |
77
|
|
|
* specified in $items. |
78
|
|
|
* |
79
|
|
|
* ```php |
80
|
|
|
* [ |
81
|
|
|
* 'groups' => [ |
82
|
|
|
* '1' => ['label' => 'Chile'], |
83
|
|
|
* '2' => ['label' => 'Russia'] |
84
|
|
|
* ], |
85
|
|
|
* ]; |
86
|
|
|
* ``` |
87
|
|
|
* |
88
|
|
|
* @param array $value |
89
|
|
|
* |
90
|
|
|
* @return self |
91
|
|
|
*/ |
92
|
1 |
|
public function groups(array $value = []): self |
93
|
|
|
{ |
94
|
1 |
|
$new = clone $this; |
95
|
1 |
|
$new->options['groups'] = $value; |
96
|
1 |
|
return $new; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The option data items. |
101
|
|
|
* |
102
|
|
|
* The array keys are option values, and the array values are the corresponding option labels. The array can also |
103
|
|
|
* be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose |
104
|
|
|
* label is the key associated with the sub-array. If you have a list of data {@see FormModel}, you may convert |
105
|
|
|
* them into the format described above using {@see \Yiisoft\Arrays\ArrayHelper::map()} |
106
|
|
|
* |
107
|
|
|
* Example: |
108
|
|
|
* ```php |
109
|
|
|
* [ |
110
|
|
|
* '1' => 'Santiago', |
111
|
|
|
* '2' => 'Concepcion', |
112
|
|
|
* '3' => 'Chillan', |
113
|
|
|
* '4' => 'Moscu' |
114
|
|
|
* '5' => 'San Petersburgo', |
115
|
|
|
* '6' => 'Novosibirsk', |
116
|
|
|
* '7' => 'Ekaterinburgo' |
117
|
|
|
* ]; |
118
|
|
|
* ``` |
119
|
|
|
* |
120
|
|
|
* Example with options groups: |
121
|
|
|
* ```php |
122
|
|
|
* [ |
123
|
|
|
* '1' => [ |
124
|
|
|
* '1' => 'Santiago', |
125
|
|
|
* '2' => 'Concepcion', |
126
|
|
|
* '3' => 'Chillan', |
127
|
|
|
* ], |
128
|
|
|
* '2' => [ |
129
|
|
|
* '4' => 'Moscu', |
130
|
|
|
* '5' => 'San Petersburgo', |
131
|
|
|
* '6' => 'Novosibirsk', |
132
|
|
|
* '7' => 'Ekaterinburgo' |
133
|
|
|
* ], |
134
|
|
|
* ]; |
135
|
|
|
* ``` |
136
|
|
|
* |
137
|
|
|
* @param array $value |
138
|
|
|
* |
139
|
|
|
* @return self |
140
|
|
|
*/ |
141
|
23 |
|
public function items(array $value): self |
142
|
|
|
{ |
143
|
23 |
|
$new = clone $this; |
144
|
23 |
|
$new->items = $value; |
145
|
23 |
|
return $new; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* The Boolean multiple attribute, if set, means the widget accepts one or more values. |
150
|
|
|
* |
151
|
|
|
* Most browsers displaying a scrolling list box for a <select> control with the multiple attribute set versus a |
152
|
|
|
* single line dropdown when the attribute is false. |
153
|
|
|
* |
154
|
|
|
* @param bool $value |
155
|
|
|
* |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
3 |
|
public function multiple(bool $value = true): self |
159
|
|
|
{ |
160
|
3 |
|
$new = clone $this; |
161
|
3 |
|
$new->options['multiple'] = $value; |
162
|
3 |
|
return $new; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Allows you to disable the widgets hidden input tag. |
167
|
|
|
* |
168
|
|
|
* @param bool $value |
169
|
|
|
* |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
5 |
|
public function noUnselect(bool $value = true): self |
173
|
|
|
{ |
174
|
5 |
|
$new = clone $this; |
175
|
5 |
|
$new->noUnselect = $value; |
176
|
5 |
|
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
|
2 |
|
public function prompt(array $value = []): self |
200
|
|
|
{ |
201
|
2 |
|
$new = clone $this; |
202
|
2 |
|
$new->options['prompt'] = $value; |
203
|
2 |
|
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
|
|
|
* Type of the input control to use. |
238
|
|
|
* |
239
|
|
|
* @param string $value |
240
|
|
|
* |
241
|
|
|
* @return self |
242
|
|
|
*/ |
243
|
4 |
|
public function type(string $value): self |
244
|
|
|
{ |
245
|
4 |
|
$new = clone $this; |
246
|
4 |
|
$new->type = $value; |
247
|
4 |
|
return $new; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* The value that should be submitted when none of the listbox is selected. |
252
|
|
|
* |
253
|
|
|
* You may set this option to be null to prevent default value submission. If this option is not set, an empty |
254
|
|
|
* string will be submitted. |
255
|
|
|
* |
256
|
|
|
* @param string $value |
257
|
|
|
* |
258
|
|
|
* @return self |
259
|
|
|
*/ |
260
|
5 |
|
public function unselect(string $value): self |
261
|
|
|
{ |
262
|
5 |
|
$new = clone $this; |
263
|
5 |
|
$new->unselect = $value; |
264
|
5 |
|
return $new; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|