Commit 61284ed4 by Qiang Xue

Fixes #3910: Removed the `container` option from `Html::checkbox()` and `Html::radio()`

parent a20f82dc
...@@ -248,10 +248,13 @@ class ActiveField extends \yii\widgets\ActiveField ...@@ -248,10 +248,13 @@ class ActiveField extends \yii\widgets\ActiveField
} }
if (!isset($options['itemOptions'])) { if (!isset($options['itemOptions'])) {
$options['itemOptions'] = [ $options['itemOptions'] = [
'container' => false,
'labelOptions' => ['class' => 'checkbox-inline'], 'labelOptions' => ['class' => 'checkbox-inline'],
]; ];
} }
} elseif (!isset($options['item'])) {
$options['item'] = function ($index, $label, $name, $checked, $value) {
return '<div class="checkbox">' . Html::checkbox($name, $checked, ['label' => $label, 'value' => $value]) . '</div>';
};
} }
parent::checkboxList($items, $options); parent::checkboxList($items, $options);
return $this; return $this;
...@@ -271,10 +274,13 @@ class ActiveField extends \yii\widgets\ActiveField ...@@ -271,10 +274,13 @@ class ActiveField extends \yii\widgets\ActiveField
} }
if (!isset($options['itemOptions'])) { if (!isset($options['itemOptions'])) {
$options['itemOptions'] = [ $options['itemOptions'] = [
'container' => false,
'labelOptions' => ['class' => 'radio-inline'], 'labelOptions' => ['class' => 'radio-inline'],
]; ];
} }
} elseif (!isset($options['item'])) {
$options['item'] = function ($index, $label, $name, $checked, $value) {
return '<div class="radio">' . Html::radio($name, $checked, ['label' => $label, 'value' => $value]) . '</div>';
};
} }
parent::radioList($items, $options); parent::radioList($items, $options);
return $this; return $this;
......
...@@ -231,6 +231,7 @@ Yii Framework 2 Change Log ...@@ -231,6 +231,7 @@ Yii Framework 2 Change Log
- Chg #3866: The `FileValidator::types` property is renamed to `FileValidator::extensions` (Ragazzo) - Chg #3866: The `FileValidator::types` property is renamed to `FileValidator::extensions` (Ragazzo)
- Chg #3897: Raised visibility of `yii\web\View::registerAssetFiles()` to protected (samdark) - Chg #3897: Raised visibility of `yii\web\View::registerAssetFiles()` to protected (samdark)
- Chg #3899: Moved `MailEvent` class to `yii\mail` namespace (cebe) - Chg #3899: Moved `MailEvent` class to `yii\mail` namespace (cebe)
- Chg #3910: Removed the `container` option from `Html::checkbox()` and `Html::radio()` (creocoder)
- Chg #3956: Flash messages set via `Yii::$app->session->setFlash()` will be removed only if they are accessed (qiangxue) - Chg #3956: Flash messages set via `Yii::$app->session->setFlash()` will be removed only if they are accessed (qiangxue)
- Chg #3989: The default value for `yii\log\FileTarget::$rotateByCopy` now defaults to true to work on windows by default (cebe) - Chg #3989: The default value for `yii\log\FileTarget::$rotateByCopy` now defaults to true to work on windows by default (cebe)
- Chg #4051: Renamed `yii\caching\GroupDependency` to `TagDependency` and added support for associating multiple tags to a single cached data item (qiangxue) - Chg #4051: Renamed `yii\caching\GroupDependency` to `TagDependency` and added support for associating multiple tags to a single cached data item (qiangxue)
......
...@@ -209,3 +209,7 @@ new ones save the following code as `convert.php` that should be placed in the s ...@@ -209,3 +209,7 @@ new ones save the following code as `convert.php` that should be placed in the s
* The signature of callbacks used in `yii\base\ArrayableTrait::fields()` is changed from `function ($field, $model) {` * The signature of callbacks used in `yii\base\ArrayableTrait::fields()` is changed from `function ($field, $model) {`
to `function ($model, $field) {`. to `function ($model, $field) {`.
* `Html::radio()`, `Html::checkbox()`, `Html::radioList()`, `Html::checkboxList()` no longer generate the container
tag around each radio/checkbox when you specify labels for them. You should manually render such container tags,
or set the `item` option for `Html::radioList()`, `Html::checkboxList()` to generate the container tags.
...@@ -620,9 +620,6 @@ class BaseHtml ...@@ -620,9 +620,6 @@ class BaseHtml
* in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks. * in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks.
* When this option is specified, the radio button will be enclosed by a label tag. * When this option is specified, the radio button will be enclosed by a label tag.
* - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option. * - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option.
* - container: array|boolean, the HTML attributes for the container tag. This is only used when the "label" option is specified.
* If it is false, no container will be rendered. If it is an array or not, a "div" container will be rendered
* around the the radio button.
* *
* The rest of the options will be rendered as the attributes of the resulting radio button tag. The values will * The rest of the options will be rendered as the attributes of the resulting radio button tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
...@@ -644,14 +641,9 @@ class BaseHtml ...@@ -644,14 +641,9 @@ class BaseHtml
if (isset($options['label'])) { if (isset($options['label'])) {
$label = $options['label']; $label = $options['label'];
$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : []; $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
$container = isset($options['container']) ? $options['container'] : ['class' => 'radio']; unset($options['label'], $options['labelOptions']);
unset($options['label'], $options['labelOptions'], $options['container']);
$content = static::label(static::input('radio', $name, $value, $options) . ' ' . $label, null, $labelOptions); $content = static::label(static::input('radio', $name, $value, $options) . ' ' . $label, null, $labelOptions);
if (is_array($container)) { return $hidden . $content;
return $hidden . static::tag('div', $content, $container);
} else {
return $hidden . $content;
}
} else { } else {
return $hidden . static::input('radio', $name, $value, $options); return $hidden . static::input('radio', $name, $value, $options);
} }
...@@ -670,9 +662,6 @@ class BaseHtml ...@@ -670,9 +662,6 @@ class BaseHtml
* in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks. * in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks.
* When this option is specified, the checkbox will be enclosed by a label tag. * When this option is specified, the checkbox will be enclosed by a label tag.
* - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option. * - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option.
* - container: array|boolean, the HTML attributes for the container tag. This is only used when the "label" option is specified.
* If it is false, no container will be rendered. If it is an array or not, a "div" container will be rendered
* around the the radio button.
* *
* The rest of the options will be rendered as the attributes of the resulting checkbox tag. The values will * The rest of the options will be rendered as the attributes of the resulting checkbox tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
...@@ -694,14 +683,9 @@ class BaseHtml ...@@ -694,14 +683,9 @@ class BaseHtml
if (isset($options['label'])) { if (isset($options['label'])) {
$label = $options['label']; $label = $options['label'];
$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : []; $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
$container = isset($options['container']) ? $options['container'] : ['class' => 'checkbox']; unset($options['label'], $options['labelOptions']);
unset($options['label'], $options['labelOptions'], $options['container']);
$content = static::label(static::input('checkbox', $name, $value, $options) . ' ' . $label, null, $labelOptions); $content = static::label(static::input('checkbox', $name, $value, $options) . ' ' . $label, null, $labelOptions);
if (is_array($container)) { return $hidden . $content;
return $hidden . static::tag('div', $content, $container);
} else {
return $hidden . $content;
}
} else { } else {
return $hidden . static::input('checkbox', $name, $value, $options); return $hidden . static::input('checkbox', $name, $value, $options);
} }
......
...@@ -217,13 +217,13 @@ class HtmlTest extends TestCase ...@@ -217,13 +217,13 @@ class HtmlTest extends TestCase
$this->assertEquals('<input type="radio" class="a" name="test" checked>', Html::radio('test', true, ['class' => 'a', 'value' => null])); $this->assertEquals('<input type="radio" class="a" name="test" checked>', Html::radio('test', true, ['class' => 'a', 'value' => null]));
$this->assertEquals('<input type="hidden" name="test" value="0"><input type="radio" class="a" name="test" value="2" checked>', Html::radio('test', true, ['class' => 'a', 'uncheck' => '0', 'value' => 2])); $this->assertEquals('<input type="hidden" name="test" value="0"><input type="radio" class="a" name="test" value="2" checked>', Html::radio('test', true, ['class' => 'a', 'uncheck' => '0', 'value' => 2]));
$this->assertEquals('<div class="radio"><label class="bbb"><input type="radio" class="a" name="test" checked> ccc</label></div>', Html::radio('test', true, [ $this->assertEquals('<label class="bbb"><input type="radio" class="a" name="test" checked> ccc</label>', Html::radio('test', true, [
'class' => 'a', 'class' => 'a',
'value' => null, 'value' => null,
'label' => 'ccc', 'label' => 'ccc',
'labelOptions' => ['class' =>'bbb'], 'labelOptions' => ['class' =>'bbb'],
])); ]));
$this->assertEquals('<input type="hidden" name="test" value="0"><div class="radio"><label><input type="radio" class="a" name="test" value="2" checked> ccc</label></div>', Html::radio('test', true, [ $this->assertEquals('<input type="hidden" name="test" value="0"><label><input type="radio" class="a" name="test" value="2" checked> ccc</label>', Html::radio('test', true, [
'class' => 'a', 'class' => 'a',
'uncheck' => '0', 'uncheck' => '0',
'label' => 'ccc', 'label' => 'ccc',
...@@ -237,13 +237,13 @@ class HtmlTest extends TestCase ...@@ -237,13 +237,13 @@ class HtmlTest extends TestCase
$this->assertEquals('<input type="checkbox" class="a" name="test" checked>', Html::checkbox('test', true, ['class' => 'a', 'value' => null])); $this->assertEquals('<input type="checkbox" class="a" name="test" checked>', Html::checkbox('test', true, ['class' => 'a', 'value' => null]));
$this->assertEquals('<input type="hidden" name="test" value="0"><input type="checkbox" class="a" name="test" value="2" checked>', Html::checkbox('test', true, ['class' => 'a', 'uncheck' => '0', 'value' => 2])); $this->assertEquals('<input type="hidden" name="test" value="0"><input type="checkbox" class="a" name="test" value="2" checked>', Html::checkbox('test', true, ['class' => 'a', 'uncheck' => '0', 'value' => 2]));
$this->assertEquals('<div class="checkbox"><label class="bbb"><input type="checkbox" class="a" name="test" checked> ccc</label></div>', Html::checkbox('test', true, [ $this->assertEquals('<label class="bbb"><input type="checkbox" class="a" name="test" checked> ccc</label>', Html::checkbox('test', true, [
'class' => 'a', 'class' => 'a',
'value' => null, 'value' => null,
'label' => 'ccc', 'label' => 'ccc',
'labelOptions' => ['class' =>'bbb'], 'labelOptions' => ['class' =>'bbb'],
])); ]));
$this->assertEquals('<input type="hidden" name="test" value="0"><div class="checkbox"><label><input type="checkbox" class="a" name="test" value="2" checked> ccc</label></div>', Html::checkbox('test', true, [ $this->assertEquals('<input type="hidden" name="test" value="0"><label><input type="checkbox" class="a" name="test" value="2" checked> ccc</label>', Html::checkbox('test', true, [
'class' => 'a', 'class' => 'a',
'uncheck' => '0', 'uncheck' => '0',
'label' => 'ccc', 'label' => 'ccc',
...@@ -338,20 +338,20 @@ EOD; ...@@ -338,20 +338,20 @@ EOD;
$this->assertEquals('<div></div>', Html::checkboxList('test')); $this->assertEquals('<div></div>', Html::checkboxList('test'));
$expected = <<<EOD $expected = <<<EOD
<div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value1"> text1</label></div> <div><label><input type="checkbox" name="test[]" value="value1"> text1</label>
<div class="checkbox"><label><input type="checkbox" name="test[]" value="value2" checked> text2</label></div></div> <label><input type="checkbox" name="test[]" value="value2" checked> text2</label></div>
EOD; EOD;
$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems())); $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value1&lt;&gt;"> text1&lt;&gt;</label></div> <div><label><input type="checkbox" name="test[]" value="value1&lt;&gt;"> text1&lt;&gt;</label>
<div class="checkbox"><label><input type="checkbox" name="test[]" value="value 2"> text 2</label></div></div> <label><input type="checkbox" name="test[]" value="value 2"> text 2</label></div>
EOD; EOD;
$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems2())); $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0"><div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value1"> text1</label></div><br> <input type="hidden" name="test" value="0"><div><label><input type="checkbox" name="test[]" value="value1"> text1</label><br>
<div class="checkbox"><label><input type="checkbox" name="test[]" value="value2" checked> text2</label></div></div> <label><input type="checkbox" name="test[]" value="value2" checked> text2</label></div>
EOD; EOD;
$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems(), [ $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems(), [
'separator' => "<br>\n", 'separator' => "<br>\n",
...@@ -374,20 +374,20 @@ EOD; ...@@ -374,20 +374,20 @@ EOD;
$this->assertEquals('<div></div>', Html::radioList('test')); $this->assertEquals('<div></div>', Html::radioList('test'));
$expected = <<<EOD $expected = <<<EOD
<div><div class="radio"><label><input type="radio" name="test" value="value1"> text1</label></div> <div><label><input type="radio" name="test" value="value1"> text1</label>
<div class="radio"><label><input type="radio" name="test" value="value2" checked> text2</label></div></div> <label><input type="radio" name="test" value="value2" checked> text2</label></div>
EOD; EOD;
$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems())); $this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<div><div class="radio"><label><input type="radio" name="test" value="value1&lt;&gt;"> text1&lt;&gt;</label></div> <div><label><input type="radio" name="test" value="value1&lt;&gt;"> text1&lt;&gt;</label>
<div class="radio"><label><input type="radio" name="test" value="value 2"> text 2</label></div></div> <label><input type="radio" name="test" value="value 2"> text 2</label></div>
EOD; EOD;
$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems2())); $this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0"><div><div class="radio"><label><input type="radio" name="test" value="value1"> text1</label></div><br> <input type="hidden" name="test" value="0"><div><label><input type="radio" name="test" value="value1"> text1</label><br>
<div class="radio"><label><input type="radio" name="test" value="value2" checked> text2</label></div></div> <label><input type="radio" name="test" value="value2" checked> text2</label></div>
EOD; EOD;
$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems(), [ $this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems(), [
'separator' => "<br>\n", 'separator' => "<br>\n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment