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 CheckBox extends Widget |
11
|
|
|
{ |
12
|
|
|
private ?string $id = null; |
13
|
|
|
private FormModelInterface $data; |
14
|
|
|
private string $attribute; |
15
|
|
|
private array $options = []; |
16
|
|
|
private string $charset = 'UTF-8'; |
17
|
|
|
private bool $enclosedByLabel = true; |
18
|
|
|
private bool $uncheck = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generates a checkbox tag together with a label for the given form attribute. |
22
|
|
|
* |
23
|
|
|
* This method will generate the "checked" tag attribute according to the form attribute value. |
24
|
|
|
* |
25
|
|
|
* @return string the generated checkbox tag. |
26
|
|
|
*/ |
27
|
15 |
|
public function run(): string |
28
|
|
|
{ |
29
|
15 |
|
return BooleanInput::widget() |
30
|
15 |
|
->type('checkbox') |
|
|
|
|
31
|
15 |
|
->id($this->id) |
32
|
15 |
|
->charset($this->charset) |
33
|
15 |
|
->config($this->data, $this->attribute, $this->options) |
34
|
15 |
|
->enclosedByLabel($this->enclosedByLabel) |
35
|
15 |
|
->uncheck($this->uncheck) |
36
|
15 |
|
->run(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set form model, name and options for the widget. |
41
|
|
|
* |
42
|
|
|
* @param FormModelInterface $data Form model. |
43
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
44
|
|
|
* @param array $options The HTML attributes for the widget container tag. |
45
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
46
|
|
|
* |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
15 |
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
50
|
|
|
{ |
51
|
15 |
|
$new = clone $this; |
52
|
15 |
|
$new->data = $data; |
53
|
15 |
|
$new->attribute = $attribute; |
54
|
15 |
|
$new->options = $options; |
55
|
15 |
|
return $new; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Focus on the control (put cursor into it) when the page loads. |
60
|
|
|
* Only one form element could be in focus at the same time. |
61
|
|
|
* |
62
|
|
|
* @param bool $value |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
1 |
|
public function autofocus(bool $value = true): self |
67
|
|
|
{ |
68
|
1 |
|
$new = clone $this; |
69
|
1 |
|
$new->options['autofocus'] = $value; |
70
|
1 |
|
return $new; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the character set used to generate the widget id. See {@see HtmlForm::getInputId()}. |
75
|
|
|
* |
76
|
|
|
* @param string $value |
77
|
|
|
* |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
1 |
|
public function charset(string $value): self |
81
|
|
|
{ |
82
|
1 |
|
$new = clone $this; |
83
|
1 |
|
$new->charset = $value; |
84
|
1 |
|
return $new; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set whether the element is disabled or not. |
89
|
|
|
* |
90
|
|
|
* If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out |
91
|
|
|
* text. |
92
|
|
|
* If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event |
93
|
|
|
* will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as |
94
|
|
|
* this will suggest you can set it to false to enable the element again, which is not the case. |
95
|
|
|
* |
96
|
|
|
* @param bool $value |
97
|
|
|
* |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
1 |
|
public function disabled(bool $value = true): self |
101
|
|
|
{ |
102
|
1 |
|
$new = clone $this; |
103
|
1 |
|
$new->options['disabled'] = $value; |
104
|
1 |
|
return $new; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the id |
109
|
|
|
* attribute of a {@see Form} element in the same document. |
110
|
|
|
* |
111
|
|
|
* @param string $value |
112
|
|
|
* |
113
|
|
|
* @return self |
114
|
|
|
*/ |
115
|
1 |
|
public function form(string $value): self |
116
|
|
|
{ |
117
|
1 |
|
$new = clone $this; |
118
|
1 |
|
$new->options['form'] = $value; |
119
|
1 |
|
return $new; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the Id of the widget. |
124
|
|
|
* |
125
|
|
|
* @param string|null $value |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
1 |
|
public function id(?string $value): self |
130
|
|
|
{ |
131
|
1 |
|
$new = clone $this; |
132
|
1 |
|
$new->id = $value; |
133
|
1 |
|
return $new; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Label displayed next to the checkbox. |
138
|
|
|
* |
139
|
|
|
* It will NOT be HTML-encoded, therefore you can pass in HTML code such as an image tag. If this is is coming from |
140
|
|
|
* end users, you should {@see encode()} it to prevent XSS attacks. |
141
|
|
|
* |
142
|
|
|
* When this option is specified, the checkbox will be enclosed by a label tag. |
143
|
|
|
* |
144
|
|
|
* @param string $value |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
1 |
|
public function label(string $value): self |
149
|
|
|
{ |
150
|
1 |
|
$new = clone $this; |
151
|
1 |
|
$new->options['label'] = $value; |
152
|
1 |
|
return $new; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* HTML attributes for the label tag. |
157
|
|
|
* |
158
|
|
|
* Do not set this option unless you set the "label" option. |
159
|
|
|
* |
160
|
|
|
* @param array $value |
161
|
|
|
* |
162
|
|
|
* @return self |
163
|
|
|
*/ |
164
|
1 |
|
public function labelOptions(array $value = []): self |
165
|
|
|
{ |
166
|
1 |
|
$new = clone $this; |
167
|
1 |
|
$new->options['labelOptions'] = $value; |
168
|
1 |
|
return $new; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* If the widget should be enclosed by label. |
173
|
|
|
* |
174
|
|
|
* @param bool $value |
175
|
|
|
* |
176
|
|
|
* @return self |
177
|
|
|
*/ |
178
|
5 |
|
public function enclosedByLabel(bool $value = true): self |
179
|
|
|
{ |
180
|
5 |
|
$new = clone $this; |
181
|
5 |
|
$new->enclosedByLabel = $value; |
182
|
5 |
|
return $new; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* If it is required to fill in a value in order to submit the form. |
187
|
|
|
* |
188
|
|
|
* @param bool $value |
189
|
|
|
* |
190
|
|
|
* @return self |
191
|
|
|
*/ |
192
|
1 |
|
public function required(bool $value = true): self |
193
|
|
|
{ |
194
|
1 |
|
$new = clone $this; |
195
|
1 |
|
$new->options['required'] = $value; |
196
|
1 |
|
return $new; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* The value associated with the uncheck state of the checkbox. |
201
|
|
|
* |
202
|
|
|
* When this attribute is present, a hidden input will be generated so that if the checkbox is not checked and |
203
|
|
|
* is submitted, the value of this attribute will still be submitted to the server via the hidden input. |
204
|
|
|
* |
205
|
|
|
* @param bool $value |
206
|
|
|
* |
207
|
|
|
* @return self |
208
|
|
|
*/ |
209
|
2 |
|
public function uncheck(bool $value = true): self |
210
|
|
|
{ |
211
|
2 |
|
$new = clone $this; |
212
|
2 |
|
$new->uncheck = $value; |
213
|
2 |
|
return $new; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|