-
Notifications
You must be signed in to change notification settings - Fork 3
/
Html.php
64 lines (61 loc) · 2.89 KB
/
Html.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace justinvoelker\awesomebootstrapcheckbox;
class Html extends \yii\helpers\Html
{
/**
* @inheritdoc
*/
public static function radio($name, $checked = false, $options = [])
{
$options['checked'] = (bool)$checked;
$value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) {
// add a hidden field so that if the radio button is not selected, it still submits a value
$hidden = static::hiddenInput($name, $options['uncheck']);
unset($options['uncheck']);
} else {
$hidden = '';
}
if (isset($options['label'])) {
$label = $options['label'];
$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
$divOptions = isset($options['divOptions']) ? $options['divOptions'] : [];
Html::addCssClass($divOptions, 'radio');
unset($options['label'], $options['labelOptions'], $options['divOptions']);
$options['id'] = str_replace(['[]', '][', '[', ']', ' '], ['', '-', '-', '', '-'], $name) . '-' . $value;
$content = Html::tag('div', static::input('radio', $name, $value, $options)
. static::label($label, $options['id'], $labelOptions), $divOptions);
return $hidden . $content;
} else {
return $hidden . static::input('radio', $name, $value, $options);
}
}
/**
* @inheritdoc
*/
public static function checkbox($name, $checked = false, $options = [])
{
$options['checked'] = (bool)$checked;
$value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) {
// add a hidden field so that if the checkbox is not selected, it still submits a value
$hidden = static::hiddenInput($name, $options['uncheck']);
unset($options['uncheck']);
} else {
$hidden = '';
}
if (isset($options['label'])) {
$label = $options['label'];
$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
$divOptions = isset($options['divOptions']) ? $options['divOptions'] : [];
Html::addCssClass($divOptions, 'checkbox');
unset($options['label'], $options['labelOptions'], $options['divOptions']);
$options['id'] = str_replace(['[]', '][', '[', ']', ' '], ['', '-', '-', '', '-'], $name) . '-' . $value;
$content = Html::tag('div', static::input('checkbox', $name, $value, $options)
. static::label($label, $options['id'], $labelOptions), $divOptions);
return $hidden . $content;
} else {
return $hidden . static::input('checkbox', $name, $value, $options);
}
}
}