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