From b99473f9685181409c1b06517bc6cc7acfebf268 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Fri, 12 Jan 2018 14:15:22 +0200 Subject: [PATCH] Methods `radioList()` and `checkboxList()` overridden at `yii\bootstrap\Html` applying Bootstrap style --- ActiveField.php | 18 +++++++++---- BaseHtml.php | 42 +++++++++++++++++++++++++++++++ CHANGELOG.md | 1 + tests/HtmlTest.php | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 129 insertions(+), 6 deletions(-) diff --git a/ActiveField.php b/ActiveField.php index 6803711..0817471 100644 --- a/ActiveField.php +++ b/ActiveField.php @@ -254,10 +254,14 @@ class ActiveField extends \yii\widgets\ActiveField '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 '
' . Html::checkbox($name, $checked, $options) . '
'; }; } @@ -284,8 +288,12 @@ class ActiveField extends \yii\widgets\ActiveField } } 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 '
' . Html::radio($name, $checked, $options) . '
'; }; } diff --git a/BaseHtml.php b/BaseHtml.php index f4cb5e9..106102c 100644 --- a/BaseHtml.php +++ b/BaseHtml.php @@ -85,4 +85,46 @@ class BaseHtml extends \yii\helpers\Html } 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 '
' . static::radio($name, $checked, $options) . '
'; + }; + } + + 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 '
' . Html::checkbox($name, $checked, $options) . '
'; + }; + } + + return parent::checkboxList($name, $selection, $items, $options); + } } diff --git a/CHANGELOG.md b/CHANGELOG.md index cdfe137..39a8143 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/tests/HtmlTest.php b/tests/HtmlTest.php index 6dd167b..309c99f 100644 --- a/tests/HtmlTest.php +++ b/tests/HtmlTest.php @@ -81,4 +81,76 @@ class HtmlTest extends TestCase { $this->assertEquals($expectedHtml, Html::staticControl($value, $options)); } -} \ No newline at end of file + + public function testRadioList() + { + $this->assertEquals('
', Html::radioList('test')); + + $dataItems = [ + 'value1' => 'text1', + 'value2' => 'text2', + ]; + + $expected = <<<'EOD' +
+
+EOD; + $this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $dataItems)); + + $expected = <<<'EOD' +
0 +1
+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' +
+EOD; + $this->assertEqualsWithoutLE($expected, Html::radioList('test', [], ['value' => 'label&'])); + + $expected = <<<'EOD' +
+EOD; + $this->assertEqualsWithoutLE($expected, Html::radioList('test', [], ['value' => 'label&'], ['encode' => false])); + } + + public function testCheckboxList() + { + $this->assertEquals('
', Html::checkboxList('test')); + + $dataItems = [ + 'value1' => 'text1', + 'value2' => 'text2', + ]; + + $expected = <<<'EOD' +
+
+EOD; + $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $dataItems)); + + $expected = <<<'EOD' +
0 +1
+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' +
+EOD; + $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', 'value', ['value' => 'label&'])); + + $expected = <<<'EOD' +
+EOD; + $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', 'value', ['value' => 'label&'], ['encode' => false])); + } +} \ No newline at end of file