1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Yiisoft\Form\FormModelInterface; |
9
|
|
|
use Yiisoft\Html\Widget\RadioList\RadioItem; |
10
|
|
|
use Yiisoft\Widget\Widget; |
11
|
|
|
|
12
|
|
|
final class RadioList extends Widget |
13
|
|
|
{ |
14
|
|
|
private FormModelInterface $data; |
15
|
|
|
private string $attribute; |
16
|
|
|
private array $options = []; |
17
|
|
|
private array $items = []; |
18
|
|
|
private bool $noUnselect = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @psalm-var Closure(RadioItem):string|null |
22
|
|
|
*/ |
23
|
|
|
private ?Closure $itemFormatter = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Generates a list of radio buttons. |
27
|
|
|
* |
28
|
|
|
* A radio button list is like a checkbox list, except that it only allows single selection. |
29
|
|
|
* |
30
|
|
|
* @return string the generated radio button list |
31
|
|
|
*/ |
32
|
12 |
|
public function run(): string |
33
|
|
|
{ |
34
|
12 |
|
return ListInput::widget() |
35
|
12 |
|
->type('radioList') |
|
|
|
|
36
|
12 |
|
->config($this->data, $this->attribute, $this->options) |
37
|
12 |
|
->items($this->items) |
38
|
12 |
|
->item($this->itemFormatter) |
39
|
12 |
|
->noUnselect($this->noUnselect) |
40
|
12 |
|
->run(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set form model, name and options for the widget. |
45
|
|
|
* |
46
|
|
|
* @param FormModelInterface $data Form model. |
47
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
48
|
|
|
* @param array $options The HTML attributes for the widget container tag. |
49
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
12 |
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
54
|
|
|
{ |
55
|
12 |
|
$new = clone $this; |
56
|
12 |
|
$new->data = $data; |
57
|
12 |
|
$new->attribute = $attribute; |
58
|
12 |
|
$new->options = $options; |
59
|
12 |
|
return $new; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Callable, a callback that can be used to customize the generation of the HTML code corresponding to a single |
64
|
|
|
* item in $items. |
65
|
|
|
* |
66
|
|
|
* The signature of this callback must be: |
67
|
|
|
* |
68
|
|
|
* ```php |
69
|
|
|
* function ($index, $label, $name, $checked, $value) |
70
|
|
|
* ``` |
71
|
|
|
* |
72
|
|
|
* @param Closure $formatter |
73
|
|
|
* |
74
|
|
|
* @return self |
75
|
|
|
*/ |
76
|
1 |
|
public function item(?Closure $formatter): self |
77
|
|
|
{ |
78
|
1 |
|
$new = clone $this; |
79
|
1 |
|
$new->itemFormatter = $formatter; |
80
|
1 |
|
return $new; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The data used to generate the list of radios. |
85
|
|
|
* |
86
|
|
|
* The array keys are the list of radio values, and the array values are the corresponding labels. |
87
|
|
|
* |
88
|
|
|
* Note that the labels will NOT be HTML-encoded, while the values will. |
89
|
|
|
* |
90
|
|
|
* @param array $value |
91
|
|
|
* |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
12 |
|
public function items(array $value = []): self |
95
|
|
|
{ |
96
|
12 |
|
$new = clone $this; |
97
|
12 |
|
$new->items = $value; |
98
|
12 |
|
return $new; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The options for generating the list of radio tag using {@see RadioList}. |
103
|
|
|
* |
104
|
|
|
* @param array $value |
105
|
|
|
* |
106
|
|
|
* @return self |
107
|
|
|
*/ |
108
|
1 |
|
public function itemOptions(array $value = []): self |
109
|
|
|
{ |
110
|
1 |
|
$new = clone $this; |
111
|
1 |
|
$new->options['itemOptions'] = $value; |
112
|
1 |
|
return $new; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Whether to HTML-encode the list of radio labels. |
117
|
|
|
* |
118
|
|
|
* Defaults to true. This option is ignored if item option is set. |
119
|
|
|
* |
120
|
|
|
* @param bool $value |
121
|
|
|
* |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
1 |
|
public function noEncode(bool $value = false): self |
125
|
|
|
{ |
126
|
1 |
|
$new = clone $this; |
127
|
1 |
|
$new->options['itemOptions']['encode'] = $value; |
128
|
1 |
|
return $new; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Allows you to disable the widgets hidden input tag. |
133
|
|
|
* |
134
|
|
|
* @param bool $value |
135
|
|
|
* |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
1 |
|
public function noUnselect(bool $value = true): self |
139
|
|
|
{ |
140
|
1 |
|
$new = clone $this; |
141
|
1 |
|
$new->noUnselect = $value; |
142
|
1 |
|
return $new; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* The HTML code that separates items. |
147
|
|
|
* |
148
|
|
|
* @param string $value |
149
|
|
|
* |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
1 |
|
public function separator(string $value = ''): self |
153
|
|
|
{ |
154
|
1 |
|
$new = clone $this; |
155
|
1 |
|
$new->options['separator'] = $value; |
156
|
1 |
|
return $new; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* The tag name of the container element. |
161
|
|
|
* |
162
|
|
|
* Null to render list of radio without container {@see Html::tag()}. |
163
|
|
|
* |
164
|
|
|
* @param string|null $value |
165
|
|
|
* |
166
|
|
|
* @return self |
167
|
|
|
*/ |
168
|
1 |
|
public function tag(?string $value = null): self |
169
|
|
|
{ |
170
|
1 |
|
$new = clone $this; |
171
|
1 |
|
$new->options['tag'] = $value; |
172
|
1 |
|
return $new; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* The value that should be submitted when none of the list of radio is selected. |
177
|
|
|
* |
178
|
|
|
* You may set this option to be null to prevent default value submission. If this option is not set, an empty |
179
|
|
|
* string will be submitted. |
180
|
|
|
* |
181
|
|
|
* @param string $value |
182
|
|
|
* |
183
|
|
|
* @return self |
184
|
|
|
*/ |
185
|
1 |
|
public function unselect(string $value = ''): self |
186
|
|
|
{ |
187
|
1 |
|
$new = clone $this; |
188
|
1 |
|
$new->options['unselect'] = $value; |
189
|
1 |
|
return $new; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|