1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
9
|
|
|
use Yiisoft\Form\FormModelInterface; |
10
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
11
|
|
|
use Yiisoft\Html\Html; |
12
|
|
|
use Yiisoft\Widget\Widget; |
13
|
|
|
|
14
|
|
|
final class BooleanInput extends Widget |
15
|
|
|
{ |
16
|
|
|
private ?string $id = null; |
17
|
|
|
private FormModelInterface $data; |
18
|
|
|
private string $attribute; |
19
|
|
|
private array $options = []; |
20
|
|
|
private string $type; |
21
|
|
|
private string $charset = 'UTF-8'; |
22
|
|
|
private bool $enclosedByLabel = true; |
23
|
|
|
private bool $uncheck = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Generates a boolean input. |
27
|
|
|
* |
28
|
|
|
* This method is mainly called by {@see CheckboxForm} and {@see RadioForm}. |
29
|
|
|
* |
30
|
|
|
* @return string the generated input element. |
31
|
|
|
*/ |
32
|
42 |
|
public function run(): string |
33
|
|
|
{ |
34
|
42 |
|
$new = clone $this; |
35
|
42 |
|
$type = $new->type; |
36
|
|
|
|
37
|
42 |
|
$tag = Html::$type($new->getName()); |
38
|
|
|
|
39
|
42 |
|
$label = ArrayHelper::remove($new->options, 'label'); |
40
|
42 |
|
if ($new->enclosedByLabel) { |
41
|
33 |
|
$label ??= $new->data->getAttributeLabel(HtmlForm::getAttributeName($new->attribute)); |
42
|
|
|
} |
43
|
42 |
|
if ($label !== null) { |
44
|
33 |
|
$labelAttributes = ArrayHelper::remove($new->options, 'labelOptions'); |
45
|
33 |
|
$tag = $tag->label($label, $labelAttributes ?? []); |
46
|
|
|
} |
47
|
|
|
|
48
|
42 |
|
unset($new->options['uncheck']); |
49
|
42 |
|
if ($new->uncheck) { |
50
|
39 |
|
$tag = $tag->uncheckValue('0'); |
51
|
|
|
} else { |
52
|
3 |
|
$uncheckValue = null; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
42 |
|
if (!empty($new->getId())) { |
56
|
42 |
|
$new->options['id'] = $new->getId(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $tag |
60
|
42 |
|
->checked($new->getBooleanValue()) |
61
|
42 |
|
->attributes($new->options) |
62
|
42 |
|
->render(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set form model, name and options for the widget. |
67
|
|
|
* |
68
|
|
|
* @param FormModelInterface $data Form model. |
69
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
70
|
|
|
* @param array $options The HTML attributes for the widget container tag. |
71
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
42 |
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
76
|
|
|
{ |
77
|
42 |
|
$new = clone $this; |
78
|
42 |
|
$new->data = $data; |
79
|
42 |
|
$new->attribute = $attribute; |
80
|
42 |
|
$new->options = $options; |
81
|
42 |
|
return $new; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Focus on the control (put cursor into it) when the page loads. |
86
|
|
|
* Only one form element could be in focus at the same time. |
87
|
|
|
* |
88
|
|
|
* It cannot be applied if the type attribute is "hidden" (that is, you cannot automatically set the cursor |
89
|
|
|
* to a hidden control). |
90
|
|
|
* |
91
|
|
|
* @param bool $value |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
1 |
|
public function autofocus(bool $value = true): self |
96
|
|
|
{ |
97
|
1 |
|
$new = clone $this; |
98
|
1 |
|
$new->options['autofocus'] = $value; |
99
|
1 |
|
return $new; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the character set used to generate the widget id. See {@see HtmlForm::getInputId()}. |
104
|
|
|
* |
105
|
|
|
* @param string $value |
106
|
|
|
* |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
16 |
|
public function charset(string $value): self |
110
|
|
|
{ |
111
|
16 |
|
$new = clone $this; |
112
|
16 |
|
$new->charset = $value; |
113
|
16 |
|
return $new; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set whether the element is disabled or not. |
118
|
|
|
* |
119
|
|
|
* If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out |
120
|
|
|
* text. |
121
|
|
|
* If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event |
122
|
|
|
* will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as |
123
|
|
|
* this will suggest you can set it to false to enable the element again, which is not the case. |
124
|
|
|
* |
125
|
|
|
* @param bool $value |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
1 |
|
public function disabled(bool $value = true): self |
130
|
|
|
{ |
131
|
1 |
|
$new = clone $this; |
132
|
1 |
|
$new->options['disabled'] = $value; |
133
|
1 |
|
return $new; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the id |
138
|
|
|
* attribute of a {@see Form} element in the same document. |
139
|
|
|
* |
140
|
|
|
* @param string $value |
141
|
|
|
* |
142
|
|
|
* @return self |
143
|
|
|
*/ |
144
|
1 |
|
public function form(string $value): self |
145
|
|
|
{ |
146
|
1 |
|
$new = clone $this; |
147
|
1 |
|
$new->options['form'] = $value; |
148
|
1 |
|
return $new; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the Id of the widget. |
153
|
|
|
* |
154
|
|
|
* @param string|null $value |
155
|
|
|
* |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
33 |
|
public function id(?string $value): self |
159
|
|
|
{ |
160
|
33 |
|
$new = clone $this; |
161
|
33 |
|
$new->id = $value; |
162
|
33 |
|
return $new; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Label displayed next to the boolean input. |
167
|
|
|
* |
168
|
|
|
* It will NOT be HTML-encoded, therefore you can pass in HTML code such as an image tag. If this is is coming from |
169
|
|
|
* end users, you should {@see encode()} it to prevent XSS attacks. |
170
|
|
|
* |
171
|
|
|
* When this option is specified, the boolean input will be enclosed by a label tag. |
172
|
|
|
* |
173
|
|
|
* @param string $value |
174
|
|
|
* |
175
|
|
|
* @return self |
176
|
|
|
*/ |
177
|
1 |
|
public function label(string $value): self |
178
|
|
|
{ |
179
|
1 |
|
$new = clone $this; |
180
|
1 |
|
$new->options['label'] = $value; |
181
|
1 |
|
return $new; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* HTML attributes for the label tag. |
186
|
|
|
* |
187
|
|
|
* Do not set this option unless you set the "label" option. |
188
|
|
|
* |
189
|
|
|
* @param array $value |
190
|
|
|
* |
191
|
|
|
* @return self |
192
|
|
|
*/ |
193
|
1 |
|
public function labelOptions(array $value = []): self |
194
|
|
|
{ |
195
|
1 |
|
$new = clone $this; |
196
|
1 |
|
$new->options['labelOptions'] = $value; |
197
|
1 |
|
return $new; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* If the widget should be enclosed by label. |
202
|
|
|
* |
203
|
|
|
* @param bool $value |
204
|
|
|
* |
205
|
|
|
* @return self |
206
|
|
|
*/ |
207
|
31 |
|
public function enclosedByLabel(bool $value = true): self |
208
|
|
|
{ |
209
|
31 |
|
$new = clone $this; |
210
|
31 |
|
$new->enclosedByLabel = $value; |
211
|
31 |
|
return $new; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* If it is required to fill in a value in order to submit the form. |
216
|
|
|
* |
217
|
|
|
* @param bool $value |
218
|
|
|
* |
219
|
|
|
* @return self |
220
|
|
|
*/ |
221
|
1 |
|
public function required(bool $value = true): self |
222
|
|
|
{ |
223
|
1 |
|
$new = clone $this; |
224
|
1 |
|
$new->options['required'] = $value; |
225
|
1 |
|
return $new; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Type of the input control to use. |
230
|
|
|
* |
231
|
|
|
* @param string $value |
232
|
|
|
* |
233
|
|
|
* @return self |
234
|
|
|
*/ |
235
|
42 |
|
public function type(string $value): self |
236
|
|
|
{ |
237
|
42 |
|
if (!in_array($value, ['checkbox', 'radio'], true)) { |
238
|
|
|
throw new InvalidArgumentException('Type should be either "checkbox" or "radio".'); |
239
|
|
|
} |
240
|
42 |
|
$new = clone $this; |
241
|
42 |
|
$new->type = $value; |
242
|
42 |
|
return $new; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* The value associated with the uncheck state of the boolean input. |
247
|
|
|
* |
248
|
|
|
* When this attribute is present, a hidden input will be generated so that if the boolean input is not checked and |
249
|
|
|
* is submitted, the value of this attribute will still be submitted to the server via the hidden input. |
250
|
|
|
* |
251
|
|
|
* @param bool $value |
252
|
|
|
* |
253
|
|
|
* @return self |
254
|
|
|
*/ |
255
|
31 |
|
public function uncheck(bool $value = false): self |
256
|
|
|
{ |
257
|
31 |
|
$new = clone $this; |
258
|
31 |
|
$new->uncheck = $value; |
259
|
31 |
|
return $new; |
260
|
|
|
} |
261
|
|
|
|
262
|
42 |
|
private function getId(): string |
263
|
|
|
{ |
264
|
42 |
|
$id = $this->options['id'] ?? $this->id; |
265
|
|
|
|
266
|
42 |
|
if ($id === null) { |
267
|
37 |
|
$id = HtmlForm::getInputId($this->data, $this->attribute, $this->charset); |
268
|
|
|
} |
269
|
|
|
|
270
|
42 |
|
return $id !== false ? (string) $id : ''; |
271
|
|
|
} |
272
|
|
|
|
273
|
42 |
|
private function getName(): string |
274
|
|
|
{ |
275
|
42 |
|
return ArrayHelper::remove($this->options, 'name', HtmlForm::getInputName($this->data, $this->attribute)); |
276
|
|
|
} |
277
|
|
|
|
278
|
42 |
|
private function getBooleanValue(): bool |
279
|
|
|
{ |
280
|
42 |
|
$value = HtmlForm::getAttributeValue($this->data, $this->attribute); |
281
|
|
|
|
282
|
42 |
|
if (!array_key_exists('value', $this->options)) { |
283
|
42 |
|
$this->options['value'] = '1'; |
284
|
|
|
} |
285
|
|
|
|
286
|
42 |
|
return (bool) $value; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|