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