mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Fixes #7449: Added encode
option to allow not encoding select options for Html::dropDownList()
and Html::listBox()
This commit is contained in:
@ -4,7 +4,6 @@ Yii Framework 2 Change Log
|
|||||||
2.0.3 under development
|
2.0.3 under development
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
- Enh #7449: `yii\helpers\Html::dropDownList()` and `yii\helpers\Html::listBox()` now supports Unicode characters in option text (yapi68)
|
|
||||||
- Bug #5457: `yii\web\Cors` should handle `Access-Control-Request-Headers` in a case-insensitive manner (qiangxue)
|
- Bug #5457: `yii\web\Cors` should handle `Access-Control-Request-Headers` in a case-insensitive manner (qiangxue)
|
||||||
- Bug #6919: Fixed wrong namespaces under advanced application's TestCase classes (ivokund)
|
- Bug #6919: Fixed wrong namespaces under advanced application's TestCase classes (ivokund)
|
||||||
- Bug #6940: `yii\web\Response::sendContentAsFile()` may not send correct `content-length` header (sadgnome)
|
- Bug #6940: `yii\web\Response::sendContentAsFile()` may not send correct `content-length` header (sadgnome)
|
||||||
@ -43,6 +42,7 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #7357: Refactored `yii\db\ColumnSchema` by adding `typecast()` method to decouple `phpTypecast()` from `dbTypecast()` (mcd-php, qiangxue)
|
- Enh #7357: Refactored `yii\db\ColumnSchema` by adding `typecast()` method to decouple `phpTypecast()` from `dbTypecast()` (mcd-php, qiangxue)
|
||||||
- Enh #7361: The `trim` validator now works on the client side too (qiangxue)
|
- Enh #7361: The `trim` validator now works on the client side too (qiangxue)
|
||||||
- Enh #7446: Added `Schema::DOUBLE` to represent ANSI SQL Double Precision type (samdark)
|
- Enh #7446: Added `Schema::DOUBLE` to represent ANSI SQL Double Precision type (samdark)
|
||||||
|
- Enh #7449: Added `encode` option to allow not encoding select options for `Html::dropDownList()` and `Html::listBox()` (yapi68, qiangxue)
|
||||||
- Enh: Added support to `yii\di\Container` to instantiate and configure an object that implements `yii\base\Configurable` (qiangxue)
|
- Enh: Added support to `yii\di\Container` to instantiate and configure an object that implements `yii\base\Configurable` (qiangxue)
|
||||||
- Chg #5690: adjusted paths in message config generated by `yii message/config` to reflect directory structure better (mikehaertl, samdark)
|
- Chg #5690: adjusted paths in message config generated by `yii message/config` to reflect directory structure better (mikehaertl, samdark)
|
||||||
- Chg #6661: Hyperlinks that are enclosed within an exist form will use the same form for submission if they specify both of the `href` and `data-method` attributes (qiangxue)
|
- Chg #6661: Hyperlinks that are enclosed within an exist form will use the same form for submission if they specify both of the `href` and `data-method` attributes (qiangxue)
|
||||||
|
@ -735,8 +735,10 @@ class BaseHtml
|
|||||||
*
|
*
|
||||||
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options',
|
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options',
|
||||||
* except that the array keys represent the optgroup labels specified in $items.
|
* except that the array keys represent the optgroup labels specified in $items.
|
||||||
|
* - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character.
|
||||||
|
* Defaults to false.
|
||||||
* - encode: bool, whether to encode option prompt and option value characters.
|
* - encode: bool, whether to encode option prompt and option value characters.
|
||||||
* Defaults to `true`.
|
* Defaults to `true`. This option is available since 2.0.3.
|
||||||
*
|
*
|
||||||
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
|
* The rest of the options will be rendered as the attributes of the resulting 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.
|
||||||
@ -785,8 +787,10 @@ class BaseHtml
|
|||||||
* - unselect: string, the value that will be submitted when no option is selected.
|
* - unselect: string, the value that will be submitted when no option is selected.
|
||||||
* When this attribute is set, a hidden field will be generated so that if no option is selected in multiple
|
* When this attribute is set, a hidden field will be generated so that if no option is selected in multiple
|
||||||
* mode, we can still obtain the posted unselect value.
|
* mode, we can still obtain the posted unselect value.
|
||||||
|
* - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character.
|
||||||
|
* Defaults to false.
|
||||||
* - encode: bool, whether to encode option prompt and option value characters.
|
* - encode: bool, whether to encode option prompt and option value characters.
|
||||||
* Defaults to `true`.
|
* Defaults to `true`. This option is available since 2.0.3.
|
||||||
*
|
*
|
||||||
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
|
* The rest of the options will be rendered as the attributes of the resulting 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.
|
||||||
@ -1373,8 +1377,10 @@ class BaseHtml
|
|||||||
*
|
*
|
||||||
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options',
|
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options',
|
||||||
* except that the array keys represent the optgroup labels specified in $items.
|
* except that the array keys represent the optgroup labels specified in $items.
|
||||||
|
* - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character.
|
||||||
|
* Defaults to false.
|
||||||
* - encode: bool, whether to encode option prompt and option value characters.
|
* - encode: bool, whether to encode option prompt and option value characters.
|
||||||
* Defaults to `true`.
|
* Defaults to `true`. This option is available since 2.0.3.
|
||||||
*
|
*
|
||||||
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
|
* The rest of the options will be rendered as the attributes of the resulting 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.
|
||||||
@ -1423,8 +1429,10 @@ class BaseHtml
|
|||||||
* - unselect: string, the value that will be submitted when no option is selected.
|
* - unselect: string, the value that will be submitted when no option is selected.
|
||||||
* When this attribute is set, a hidden field will be generated so that if no option is selected in multiple
|
* When this attribute is set, a hidden field will be generated so that if no option is selected in multiple
|
||||||
* mode, we can still obtain the posted unselect value.
|
* mode, we can still obtain the posted unselect value.
|
||||||
|
* - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character.
|
||||||
|
* Defaults to false.
|
||||||
* - encode: bool, whether to encode option prompt and option value characters.
|
* - encode: bool, whether to encode option prompt and option value characters.
|
||||||
* Defaults to `true`.
|
* Defaults to `true`. This option is available since 2.0.3.
|
||||||
*
|
*
|
||||||
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
|
* The rest of the options will be rendered as the attributes of the resulting 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.
|
||||||
@ -1558,22 +1566,27 @@ class BaseHtml
|
|||||||
public static function renderSelectOptions($selection, $items, &$tagOptions = [])
|
public static function renderSelectOptions($selection, $items, &$tagOptions = [])
|
||||||
{
|
{
|
||||||
$lines = [];
|
$lines = [];
|
||||||
|
$encodeSpaces = ArrayHelper::remove($tagOptions, 'encodeSpaces', false);
|
||||||
$encode = ArrayHelper::remove($tagOptions, 'encode', true);
|
$encode = ArrayHelper::remove($tagOptions, 'encode', true);
|
||||||
if (isset($tagOptions['prompt'])) {
|
if (isset($tagOptions['prompt'])) {
|
||||||
$prompt = $encode ? static::encode($tagOptions['prompt']) : $tagOptions['prompt'];
|
$prompt = $encode ? static::encode($tagOptions['prompt']) : $tagOptions['prompt'];
|
||||||
|
if ($encodeSpaces) {
|
||||||
|
$prompt = str_replace(' ', ' ', $prompt);
|
||||||
|
}
|
||||||
$lines[] = static::tag('option', $prompt, ['value' => '']);
|
$lines[] = static::tag('option', $prompt, ['value' => '']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$options = isset($tagOptions['options']) ? $tagOptions['options'] : [];
|
$options = isset($tagOptions['options']) ? $tagOptions['options'] : [];
|
||||||
$groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : [];
|
$groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : [];
|
||||||
unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']);
|
unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']);
|
||||||
|
$options['encodeSpaces'] = ArrayHelper::getValue($options, 'encodeSpaces', $encodeSpaces);
|
||||||
$options['encode'] = ArrayHelper::getValue($options, 'encode', $encode);
|
$options['encode'] = ArrayHelper::getValue($options, 'encode', $encode);
|
||||||
|
|
||||||
foreach ($items as $key => $value) {
|
foreach ($items as $key => $value) {
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$groupAttrs = isset($groups[$key]) ? $groups[$key] : [];
|
$groupAttrs = isset($groups[$key]) ? $groups[$key] : [];
|
||||||
$groupAttrs['label'] = $key;
|
$groupAttrs['label'] = $key;
|
||||||
$attrs = ['options' => $options, 'groups' => $groups, 'encode' => $encode];
|
$attrs = ['options' => $options, 'groups' => $groups, 'encodeSpaces' => $encodeSpaces, 'encode' => $encode];
|
||||||
$content = static::renderSelectOptions($selection, $value, $attrs);
|
$content = static::renderSelectOptions($selection, $value, $attrs);
|
||||||
$lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs);
|
$lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs);
|
||||||
} else {
|
} else {
|
||||||
@ -1582,7 +1595,11 @@ class BaseHtml
|
|||||||
$attrs['selected'] = $selection !== null &&
|
$attrs['selected'] = $selection !== null &&
|
||||||
(!is_array($selection) && !strcmp($key, $selection)
|
(!is_array($selection) && !strcmp($key, $selection)
|
||||||
|| is_array($selection) && in_array($key, $selection));
|
|| is_array($selection) && in_array($key, $selection));
|
||||||
$lines[] = static::tag('option', ($encode ? static::encode($value) : $value), $attrs);
|
$text = $encode ? static::encode($value) : $value;
|
||||||
|
if ($encodeSpaces) {
|
||||||
|
$text = str_replace(' ', ' ', $text);
|
||||||
|
}
|
||||||
|
$lines[] = static::tag('option', $text, $attrs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,7 +296,21 @@ EOD;
|
|||||||
<option value="value 2">text 2</option>
|
<option value="value 2">text 2</option>
|
||||||
</select>
|
</select>
|
||||||
EOD;
|
EOD;
|
||||||
$this->assertEqualsWithoutLE($expected, Html::listBox('test', null, $this->getDataItems2(), ['encode' => true]));
|
$this->assertEqualsWithoutLE($expected, Html::listBox('test', null, $this->getDataItems2(), ['encodeSpaces' => true]));
|
||||||
|
$expected = <<<EOD
|
||||||
|
<select name="test" size="4">
|
||||||
|
<option value="value1<>">text1<></option>
|
||||||
|
<option value="value 2">text 2</option>
|
||||||
|
</select>
|
||||||
|
EOD;
|
||||||
|
$this->assertEqualsWithoutLE($expected, Html::listBox('test', null, $this->getDataItems2(), ['encode' => false]));
|
||||||
|
$expected = <<<EOD
|
||||||
|
<select name="test" size="4">
|
||||||
|
<option value="value1<>">text1<></option>
|
||||||
|
<option value="value 2">text 2</option>
|
||||||
|
</select>
|
||||||
|
EOD;
|
||||||
|
$this->assertEqualsWithoutLE($expected, Html::listBox('test', null, $this->getDataItems2(), ['encodeSpaces' => true, 'encode' => false]));
|
||||||
$expected = <<<EOD
|
$expected = <<<EOD
|
||||||
<select name="test" size="4">
|
<select name="test" size="4">
|
||||||
<option value="value1">text1</option>
|
<option value="value1">text1</option>
|
||||||
@ -499,7 +513,7 @@ EOD;
|
|||||||
'groups' => [
|
'groups' => [
|
||||||
'group12' => ['class' => 'group'],
|
'group12' => ['class' => 'group'],
|
||||||
],
|
],
|
||||||
'encode' => true,
|
'encodeSpaces' => true,
|
||||||
];
|
];
|
||||||
$this->assertEqualsWithoutLE($expected, Html::renderSelectOptions(['value111', 'value1'], $data, $attributes));
|
$this->assertEqualsWithoutLE($expected, Html::renderSelectOptions(['value111', 'value1'], $data, $attributes));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user