Browse Source

Methods `radioList()` and `checkboxList()` overridden at `yii\bootstrap\Html` applying Bootstrap style

tags/2.0.8
Klimov Paul 7 years ago
parent
commit
b99473f968
  1. 18
      ActiveField.php
  2. 42
      BaseHtml.php
  3. 1
      CHANGELOG.md
  4. 72
      tests/HtmlTest.php

18
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 '<div class="checkbox">' . Html::checkbox($name, $checked, $options) . '</div>';
};
}
@ -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 '<div class="radio">' . Html::radio($name, $checked, $options) . '</div>';
};
}

42
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 '<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
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)

72
tests/HtmlTest.php

@ -81,4 +81,76 @@ class HtmlTest extends TestCase
{
$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]));
}
}
Loading…
Cancel
Save