|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\kissform\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\kissform\Field; |
|
6
|
|
|
use mindplay\kissform\InputModel; |
|
7
|
|
|
use mindplay\kissform\InputRenderer; |
|
8
|
|
|
use mindplay\kissform\Validators\CheckSelected; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This class provides information about a group of `<input type="radio">` elements. |
|
12
|
|
|
* |
|
13
|
|
|
* Note that the markup deviates from Bootstrap standard markup, which isn't useful for styling. |
|
14
|
|
|
* |
|
15
|
|
|
* @link https://github.com/twbs/bootstrap/issues/19931 |
|
16
|
|
|
* @link https://github.com/flatlogic/awesome-bootstrap-checkbox |
|
17
|
|
|
*/ |
|
18
|
|
|
class RadioGroup extends Field |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string[] map where option values map to option labels |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $options; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string[] map of HTML attributes for the <input> tag |
|
27
|
|
|
*/ |
|
28
|
|
|
public $input_attr = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string[] map of HTML attributes for the <label> tag |
|
32
|
|
|
*/ |
|
33
|
|
|
public $label_attr = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string|null wrapper tag (e.g. "div", or NULL to disable wrapper-tags) |
|
37
|
|
|
*/ |
|
38
|
|
|
public $wrapper_tag = 'div'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string[] map of HTML attributes for the wrapper-tag around each checkbox group |
|
42
|
|
|
*/ |
|
43
|
|
|
public $wrapper_attr = ['class' => 'radio']; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $name field name |
|
47
|
|
|
* @param string[] $options map where option values map to option labels |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function __construct($name, array $options) |
|
50
|
|
|
{ |
|
51
|
2 |
|
parent::__construct($name); |
|
52
|
|
|
|
|
53
|
2 |
|
$this->options = $options; |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritdoc |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function createValidators() |
|
60
|
|
|
{ |
|
61
|
1 |
|
return [new CheckSelected(array_keys($this->options))]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
|
68
|
|
|
{ |
|
69
|
1 |
|
$selected = $model->getInput($this); |
|
70
|
|
|
|
|
71
|
1 |
|
$html = ''; |
|
72
|
|
|
|
|
73
|
1 |
|
foreach ($this->options as $value => $label) { |
|
74
|
1 |
|
$checked = is_numeric($selected) |
|
75
|
1 |
|
? $value == $selected // loose comparison works well for NULLs and numbers |
|
76
|
1 |
|
: $value === $selected; // strict comparison for everything else |
|
77
|
|
|
|
|
78
|
1 |
|
$id = $renderer->getId($this, $value); |
|
79
|
|
|
|
|
80
|
1 |
|
$group = $renderer->tag( |
|
81
|
1 |
|
'input', |
|
82
|
1 |
|
$renderer->mergeAttrs( |
|
83
|
|
|
[ |
|
84
|
1 |
|
'type' => 'radio', |
|
85
|
1 |
|
'id' => $id, |
|
86
|
1 |
|
'name' => $renderer->getName($this), |
|
87
|
1 |
|
'value' => $value, |
|
88
|
1 |
|
'checked' => $checked, |
|
89
|
|
|
], |
|
90
|
1 |
|
$this->input_attr, |
|
91
|
1 |
|
$attr |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
1 |
|
$group .= $renderer->tag('label', $this->label_attr + ['for' => $id], $renderer->softEscape($label)); |
|
96
|
|
|
|
|
97
|
1 |
|
if ($this->wrapper_tag) { |
|
|
|
|
|
|
98
|
1 |
|
$group = $renderer->tag($this->wrapper_tag, $this->wrapper_attr, $group); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$html .= $group; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
return $html; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: