Skip to content

Commit

Permalink
Methods radioList() and checkboxList() overridden at `yii\bootstr…
Browse files Browse the repository at this point in the history
…ap\Html` applying Bootstrap style
  • Loading branch information
klimov-paul committed Jan 12, 2018
1 parent f245430 commit b99473f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 6 deletions.
18 changes: 13 additions & 5 deletions ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,14 @@ public function checkboxList($items, $options = [])
'labelOptions' => ['class' => 'checkbox-inline'],
];
}
} elseif (!isset($options['item'])) {
} elseif (!isset($options['item'])) {
$itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : [];
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions) {
$options = array_merge(['label' => $label, 'value' => $value], $itemOptions);
$encode = ArrayHelper::getValue($options, 'encode', true);
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
$options = array_merge([
'label' => $encode ? Html::encode($label) : $label,
'value' => $value
], $itemOptions);
return '<div class="checkbox">' . Html::checkbox($name, $checked, $options) . '</div>';
};
}
Expand All @@ -284,8 +288,12 @@ public function radioList($items, $options = [])
}
} elseif (!isset($options['item'])) {
$itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : [];
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions) {
$options = array_merge(['label' => $label, 'value' => $value], $itemOptions);
$encode = ArrayHelper::getValue($options, 'encode', true);
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
$options = array_merge([
'label' => $encode ? Html::encode($label) : $label,
'value' => $value
], $itemOptions);
return '<div class="radio">' . Html::radio($name, $checked, $options) . '</div>';
};
}
Expand Down
42 changes: 42 additions & 0 deletions BaseHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,46 @@ public static function activeStaticControl($model, $attribute, $options = [])
}
return static::staticControl($value, $options);
}

/**
* {@inheritdoc}
* @since 2.0.8
*/
public static function radioList($name, $selection = null, $items = [], $options = [])
{
if (!isset($options['item'])) {
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
$encode = ArrayHelper::getValue($options, 'encode', true);
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
$options = array_merge([
'label' => $encode ? static::encode($label) : $label,
'value' => $value
], $itemOptions);
return '<div class="radio">' . static::radio($name, $checked, $options) . '</div>';
};
}

return parent::radioList($name, $selection, $items, $options);
}

/**
* {@inheritdoc}
* @since 2.0.8
*/
public static function checkboxList($name, $selection = null, $items = [], $options = [])
{
if (!isset($options['item'])) {
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
$encode = ArrayHelper::getValue($options, 'encode', true);
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
$options = array_merge([
'label' => $encode ? static::encode($label) : $label,
'value' => $value
], $itemOptions);
return '<div class="checkbox">' . Html::checkbox($name, $checked, $options) . '</div>';
};
}

return parent::checkboxList($name, $selection, $items, $options);
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Yii Framework 2 bootstrap extension Change Log

- Bug #126: Fixed `yii\bootstrap\ToggleButtonGroup` toggles dropdown for both buttons in case `split` is enabled (klimov-paul)
- Bug #136: Allow overriding `horizontalCssClasses` when extending `\yii\bootstrap\ActiveField` (mikehaertl, klimov-paul)
- Enh #118: Methods `radioList()` and `checkboxList()` overridden at `yii\bootstrap\Html` applying Bootstrap style (klimov-paul)
- Enh #219: Add ability to use custom HTML in navbar-header (razvanphp)
- Enh #171: Add ability to use a brandImage with the navbar (razvanphp)
- Enh #227: Added `yii\bootstrap\Collapse::$itemToggleOptions` allowing setup custom collapse tag name and HTML options (mskayali, klimov-paul)
Expand Down
74 changes: 73 additions & 1 deletion tests/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,76 @@ public function testStaticControl($value, array $options, $expectedHtml)
{
$this->assertEquals($expectedHtml, Html::staticControl($value, $options));
}
}

public function testRadioList()
{
$this->assertEquals('<div></div>', Html::radioList('test'));

$dataItems = [
'value1' => 'text1',
'value2' => 'text2',
];

$expected = <<<'EOD'
<div><div class="radio"><label><input type="radio" name="test" value="value1"> text1</label></div>
<div class="radio"><label><input type="radio" name="test" value="value2" checked> text2</label></div></div>
EOD;
$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $dataItems));

$expected = <<<'EOD'
<div>0<label>text1 <input type="radio" name="test" value="value1"></label>
1<label>text2 <input type="radio" name="test" value="value2" checked></label></div>
EOD;
$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $dataItems, [
'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::radio($name, $checked, ['value' => $value]));
},
]));

$expected = <<<'EOD'
<div><div class="radio"><label><input type="radio" name="test" value="value"> label&amp;</label></div></div>
EOD;
$this->assertEqualsWithoutLE($expected, Html::radioList('test', [], ['value' => 'label&']));

$expected = <<<'EOD'
<div><div class="radio"><label><input type="radio" name="test" value="value"> label&</label></div></div>
EOD;
$this->assertEqualsWithoutLE($expected, Html::radioList('test', [], ['value' => 'label&'], ['encode' => false]));
}

public function testCheckboxList()
{
$this->assertEquals('<div></div>', Html::checkboxList('test'));

$dataItems = [
'value1' => 'text1',
'value2' => 'text2',
];

$expected = <<<'EOD'
<div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value1"> text1</label></div>
<div class="checkbox"><label><input type="checkbox" name="test[]" value="value2" checked> text2</label></div></div>
EOD;
$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $dataItems));

$expected = <<<'EOD'
<div>0<label>text1 <input type="checkbox" name="test[]" value="value1"></label>
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked></label></div>
EOD;
$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $dataItems, [
'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, ['value' => $value]));
},
]));

$expected = <<<'EOD'
<div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value" checked> label&amp;</label></div></div>
EOD;
$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', 'value', ['value' => 'label&']));

$expected = <<<'EOD'
<div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value" checked> label&</label></div></div>
EOD;
$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', 'value', ['value' => 'label&'], ['encode' => false]));
}
}

0 comments on commit b99473f

Please sign in to comment.