1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Stringable; |
9
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
10
|
|
|
use Yiisoft\Form\Widget\Attribute\InputAttributes; |
11
|
|
|
use Yiisoft\Form\Widget\Attribute\ModelAttributes; |
12
|
|
|
use Yiisoft\Html\Tag\Input\Radio as RadioTag; |
13
|
|
|
use Yiisoft\Widget\Widget; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The input element with a type attribute whose value is "radio" represents a selection of one item from a list of |
17
|
|
|
* items (a radio button). |
18
|
|
|
* |
19
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.radio.html |
20
|
|
|
*/ |
21
|
|
|
final class Radio extends Widget |
22
|
|
|
{ |
23
|
|
|
use InputAttributes; |
24
|
|
|
use ModelAttributes; |
25
|
|
|
|
26
|
|
|
private bool $enclosedByLabel = true; |
27
|
|
|
private string $label = ''; |
28
|
|
|
private array $labelAttributes = []; |
29
|
|
|
/** @var scalar|Stringable|null */ |
30
|
|
|
private $value = null; |
31
|
|
|
private ?string $uncheckValue = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* If the widget should be enclosed by label. |
35
|
|
|
* |
36
|
|
|
* @param bool $value If the widget should be en closed by label. |
37
|
|
|
* |
38
|
|
|
* @return static |
39
|
|
|
*/ |
40
|
11 |
|
public function enclosedByLabel(bool $value = true): self |
41
|
|
|
{ |
42
|
11 |
|
$new = clone $this; |
43
|
11 |
|
$new->enclosedByLabel = $value; |
44
|
11 |
|
return $new; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Label displayed next to the radio. |
49
|
|
|
* |
50
|
|
|
* It will NOT be HTML-encoded, therefore you can pass in HTML code such as an image tag. If this is coming from |
51
|
|
|
* end users, you should {@see encode()} it to prevent XSS attacks. |
52
|
|
|
* |
53
|
|
|
* When this option is specified, the radio will be enclosed by a label tag. |
54
|
|
|
* |
55
|
|
|
* @param string $value |
56
|
|
|
* |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
3 |
|
public function label(string $value): self |
60
|
|
|
{ |
61
|
3 |
|
$new = clone $this; |
62
|
3 |
|
$new->label = $value; |
63
|
3 |
|
return $new; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* HTML attributes for the label tag. |
68
|
|
|
* |
69
|
|
|
* Do not set this option unless you set the "label" option. |
70
|
|
|
* |
71
|
|
|
* @param array $value |
72
|
|
|
* |
73
|
|
|
* @return static |
74
|
|
|
*/ |
75
|
3 |
|
public function labelAttributes(array $value = []): self |
76
|
|
|
{ |
77
|
3 |
|
$new = clone $this; |
78
|
3 |
|
$new->labelAttributes = $value; |
79
|
3 |
|
return $new; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param bool|float|int|string|Stringable|null $value Value that corresponds to "unchecked" state of the input. |
84
|
|
|
* |
85
|
|
|
* @return static |
86
|
|
|
*/ |
87
|
2 |
|
public function uncheckValue($value): self |
88
|
|
|
{ |
89
|
2 |
|
$new = clone $this; |
90
|
2 |
|
$new->uncheckValue = $value === null ? null : (string) $value; |
91
|
2 |
|
return $new; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Generates a radio button tag together with a label for the given form attribute. |
96
|
|
|
* |
97
|
|
|
* @return string the generated radio button tag. |
98
|
|
|
*/ |
99
|
16 |
|
protected function run(): string |
100
|
|
|
{ |
101
|
16 |
|
$new = clone $this; |
102
|
16 |
|
$radio = RadioTag::tag(); |
103
|
|
|
|
104
|
16 |
|
$value = HtmlForm::getAttributeValue($new->getFormModel(), $new->attribute); |
105
|
|
|
|
106
|
16 |
|
if (is_iterable($value) || is_object($value)) { |
107
|
2 |
|
throw new InvalidArgumentException('Radio widget value can not be an iterable or an object.'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** @var scalar|Stringable|null */ |
111
|
14 |
|
$valueDefault = array_key_exists('value', $new->attributes) ? $new->attributes['value'] : $new->value; |
112
|
|
|
|
113
|
14 |
|
if ($new->enclosedByLabel === true) { |
114
|
12 |
|
$label = $new->label !== '' |
115
|
12 |
|
? $new->label : HtmlForm::getAttributeLabel($new->getFormModel(), $new->attribute); |
116
|
12 |
|
$radio = $radio->label($label, $new->labelAttributes); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $radio |
120
|
14 |
|
->checked("$value" === "$valueDefault") |
121
|
14 |
|
->attributes($new->attributes) |
122
|
14 |
|
->id($new->getId()) |
123
|
14 |
|
->name(HtmlForm::getInputName($new->getFormModel(), $new->attribute)) |
124
|
14 |
|
->uncheckValue($new->uncheckValue) |
125
|
14 |
|
->value(is_bool($valueDefault) ? (int) $valueDefault : $valueDefault) |
126
|
14 |
|
->render(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|