1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Stringable; |
10
|
|
|
use Yiisoft\Form\Field\Base\PartsField; |
11
|
|
|
use Yiisoft\Form\Field\Base\FormAttributeTrait; |
12
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; |
13
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait; |
14
|
|
|
use Yiisoft\Form\Field\Part\Error; |
15
|
|
|
use Yiisoft\Form\Field\Part\Hint; |
16
|
|
|
use Yiisoft\Form\Field\Part\Label; |
17
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
18
|
|
|
use Yiisoft\Html\Widget\CheckboxList\CheckboxItem; |
19
|
|
|
use Yiisoft\Html\Widget\CheckboxList\CheckboxList as CheckboxListWidget; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @see CheckboxListWidget |
23
|
|
|
*/ |
24
|
|
|
final class CheckboxList extends PartsField implements ValidationClassInterface |
25
|
|
|
{ |
26
|
|
|
use FormAttributeTrait; |
27
|
|
|
use ValidationClassTrait; |
28
|
|
|
|
29
|
|
|
private CheckboxListWidget $widget; |
30
|
|
|
|
31
|
2 |
|
public function __construct() |
32
|
|
|
{ |
33
|
2 |
|
$this->widget = CheckboxListWidget::create(''); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function checkboxAttributes(array $attributes): self |
37
|
|
|
{ |
38
|
|
|
$new = clone $this; |
39
|
|
|
$new->widget = $this->widget->checkboxAttributes($attributes); |
40
|
|
|
return $new; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function replaceCheckboxAttributes(array $attributes): self |
44
|
|
|
{ |
45
|
|
|
$new = clone $this; |
46
|
|
|
$new->widget = $this->widget->replaceCheckboxAttributes($attributes); |
47
|
|
|
return $new; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array[] $attributes |
52
|
|
|
*/ |
53
|
|
|
public function individualInputAttributes(array $attributes): self |
54
|
|
|
{ |
55
|
|
|
$new = clone $this; |
56
|
|
|
$new->widget = $this->widget->individualInputAttributes($attributes); |
57
|
|
|
return $new; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array[] $attributes |
62
|
|
|
*/ |
63
|
|
|
public function replaceIndividualInputAttributes(array $attributes): self |
64
|
|
|
{ |
65
|
|
|
$new = clone $this; |
66
|
|
|
$new->widget = $this->widget->replaceIndividualInputAttributes($attributes); |
67
|
|
|
return $new; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string[] $items |
72
|
|
|
* @param bool $encodeLabels Whether labels should be encoded. |
73
|
|
|
*/ |
74
|
2 |
|
public function items(array $items, bool $encodeLabels = true): self |
75
|
|
|
{ |
76
|
2 |
|
$new = clone $this; |
77
|
2 |
|
$new->widget = $this->widget->items($items, $encodeLabels); |
78
|
2 |
|
return $new; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Fills items from an array provided. Array values are used for both input labels and input values. |
83
|
|
|
* |
84
|
|
|
* @param bool[]|float[]|int[]|string[]|Stringable[] $values |
85
|
|
|
* @param bool $encodeLabels Whether labels should be encoded. |
86
|
|
|
*/ |
87
|
|
|
public function itemsFromValues(array $values, bool $encodeLabels = true): self |
88
|
|
|
{ |
89
|
|
|
$new = clone $this; |
90
|
|
|
$new->widget = $this->widget->itemsFromValues($values, $encodeLabels); |
91
|
|
|
return $new; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the ID |
96
|
|
|
* attribute of a form element in the same document. |
97
|
|
|
* |
98
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
99
|
|
|
*/ |
100
|
|
|
public function form(?string $formId): self |
101
|
|
|
{ |
102
|
|
|
$new = clone $this; |
103
|
|
|
$new->widget = $this->widget->form($formId); |
104
|
|
|
return $new; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param bool $disabled Whether checkboxes is disabled. |
109
|
|
|
* |
110
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled |
111
|
|
|
*/ |
112
|
|
|
public function disabled(bool $disabled = true): self |
113
|
|
|
{ |
114
|
|
|
$new = clone $this; |
115
|
|
|
$new->widget = $this->widget->disabled($disabled); |
116
|
|
|
return $new; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function uncheckValue(bool|float|int|string|Stringable|null $value): self |
120
|
|
|
{ |
121
|
|
|
$new = clone $this; |
122
|
|
|
$new->widget = $this->widget->uncheckValue($value); |
123
|
|
|
return $new; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function separator(string $separator): self |
127
|
|
|
{ |
128
|
|
|
$new = clone $this; |
129
|
|
|
$new->widget = $this->widget->separator($separator); |
130
|
|
|
return $new; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Closure|null $formatter |
135
|
|
|
* |
136
|
|
|
* @psalm-param Closure(CheckboxItem):string|null $formatter |
137
|
|
|
*/ |
138
|
|
|
public function itemFormatter(?Closure $formatter): self |
139
|
|
|
{ |
140
|
|
|
$new = clone $this; |
141
|
|
|
$new->widget = $this->widget->itemFormatter($formatter); |
142
|
|
|
return $new; |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
protected function generateInput(): string |
146
|
|
|
{ |
147
|
2 |
|
$value = $this->getAttributeValue(); |
148
|
|
|
|
149
|
|
|
/** @var mixed $value */ |
150
|
2 |
|
$value ??= []; |
151
|
2 |
|
if (!is_iterable($value)) { |
152
|
|
|
throw new InvalidArgumentException( |
153
|
|
|
'"CheckboxList" field requires iterable or null value.' |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
/** @psalm-var iterable<int, Stringable|scalar> $value */ |
157
|
|
|
|
158
|
2 |
|
return $this->widget |
159
|
2 |
|
->name($this->getInputName()) |
160
|
2 |
|
->values($value) |
161
|
2 |
|
->render(); |
162
|
|
|
} |
163
|
|
|
|
164
|
2 |
|
protected function renderLabel(Label $label): string |
165
|
|
|
{ |
166
|
|
|
return $label |
167
|
2 |
|
->attribute($this->getFormModel(), $this->attribute) |
168
|
2 |
|
->useInputIdAttribute(false) |
169
|
2 |
|
->render(); |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
protected function renderHint(Hint $hint): string |
173
|
|
|
{ |
174
|
|
|
return $hint |
175
|
2 |
|
->attribute($this->getFormModel(), $this->attribute) |
176
|
2 |
|
->render(); |
177
|
|
|
} |
178
|
|
|
|
179
|
2 |
|
protected function renderError(Error $error): string |
180
|
|
|
{ |
181
|
|
|
return $error |
182
|
2 |
|
->attribute($this->getFormModel(), $this->attribute) |
183
|
2 |
|
->render(); |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
protected function prepareContainerTagAttributes(array &$attributes): void |
187
|
|
|
{ |
188
|
2 |
|
if ($this->hasFormModelAndAttribute()) { |
189
|
2 |
|
$this->addValidationClassToTagAttributes( |
190
|
|
|
$attributes, |
191
|
2 |
|
$this->getFormModel(), |
192
|
2 |
|
$this->getAttributeName(), |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
private function getInputName(): string |
198
|
|
|
{ |
199
|
2 |
|
return HtmlForm::getInputName($this->getFormModel(), $this->attribute); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|