Merge branch 'arogachev-7420-dropdown-prompt-attributes'

This commit is contained in:
SilverFire - Dmitry Naumenko
2016-11-17 16:43:47 +02:00
3 changed files with 58 additions and 8 deletions

View File

@ -22,6 +22,7 @@ Yii Framework 2 Change Log
- Bug #12939: Hard coded table names for MSSQL in RBAC migration (arogachev) - Bug #12939: Hard coded table names for MSSQL in RBAC migration (arogachev)
- Bug #12974: Fixed incorrect order of migrations history in case `yii\console\controllers\MigrateController::$migrationNamespaces` is in use (evgen-d, klimov-paul) - Bug #12974: Fixed incorrect order of migrations history in case `yii\console\controllers\MigrateController::$migrationNamespaces` is in use (evgen-d, klimov-paul)
- Enh #6809: Added `\yii\caching\Cache::$defaultDuration` property, allowing to set custom default cache duration (sdkiller) - Enh #6809: Added `\yii\caching\Cache::$defaultDuration` property, allowing to set custom default cache duration (sdkiller)
- Enh #7420: Attributes for prompt generated with `renderSelectOptions` of `\yii\helpers\Html` helper (arogachev)
- Enh #11037: `yii.js` and `yii.validation.js` use `Regexp.test()` instead of `String.match()` (arogachev, nkovacs) - Enh #11037: `yii.js` and `yii.validation.js` use `Regexp.test()` instead of `String.match()` (arogachev, nkovacs)
- Enh #11756: Added type mapping for `varbinary` data type in MySQL DBMS (silverfire) - Enh #11756: Added type mapping for `varbinary` data type in MySQL DBMS (silverfire)
- Enh #11929: Changed `type` column type from `int` to `smallInt` in RBAC migrations (silverfire) - Enh #11929: Changed `type` column type from `int` to `smallInt` in RBAC migrations (silverfire)

View File

@ -754,7 +754,13 @@ class BaseHtml
* the labels will also be HTML-encoded. * the labels will also be HTML-encoded.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - prompt: string, a prompt text to be displayed as the first option; * - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array
* to override the value and to set other tag attributes:
*
* ```php
* ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
* ```
*
* - options: array, the attributes for the select option tags. The array keys must be valid option values, * - options: array, the attributes for the select option tags. The array keys must be valid option values,
* and the array values are the extra attributes for the corresponding option tags. For example, * and the array values are the extra attributes for the corresponding option tags. For example,
* *
@ -803,7 +809,13 @@ class BaseHtml
* the labels will also be HTML-encoded. * the labels will also be HTML-encoded.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - prompt: string, a prompt text to be displayed as the first option; * - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array
* to override the value and to set other tag attributes:
*
* ```php
* ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
* ```
*
* - options: array, the attributes for the select option tags. The array keys must be valid option values, * - options: array, the attributes for the select option tags. The array keys must be valid option values,
* and the array values are the extra attributes for the corresponding option tags. For example, * and the array values are the extra attributes for the corresponding option tags. For example,
* *
@ -1477,7 +1489,13 @@ class BaseHtml
* the labels will also be HTML-encoded. * the labels will also be HTML-encoded.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - prompt: string, a prompt text to be displayed as the first option; * - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array
* to override the value and to set other tag attributes:
*
* ```php
* ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
* ```
*
* - options: array, the attributes for the select option tags. The array keys must be valid option values, * - options: array, the attributes for the select option tags. The array keys must be valid option values,
* and the array values are the extra attributes for the corresponding option tags. For example, * and the array values are the extra attributes for the corresponding option tags. For example,
* *
@ -1526,7 +1544,13 @@ class BaseHtml
* the labels will also be HTML-encoded. * the labels will also be HTML-encoded.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
* *
* - prompt: string, a prompt text to be displayed as the first option; * - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array
* to override the value and to set other tag attributes:
*
* ```php
* ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
* ```
*
* - options: array, the attributes for the select option tags. The array keys must be valid option values, * - options: array, the attributes for the select option tags. The array keys must be valid option values,
* and the array values are the extra attributes for the corresponding option tags. For example, * and the array values are the extra attributes for the corresponding option tags. For example,
* *
@ -1693,11 +1717,18 @@ class BaseHtml
$encodeSpaces = ArrayHelper::remove($tagOptions, 'encodeSpaces', false); $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']; $promptOptions = ['value' => ''];
if ($encodeSpaces) { if (is_string($tagOptions['prompt'])) {
$prompt = str_replace(' ', ' ', $prompt); $promptText = $tagOptions['prompt'];
} else {
$promptText = $tagOptions['prompt']['text'];
$promptOptions = array_merge($promptOptions, $tagOptions['prompt']['options']);
} }
$lines[] = static::tag('option', $prompt, ['value' => '']); $promptText = $encode ? static::encode($promptText) : $promptText;
if ($encodeSpaces) {
$promptText = str_replace(' ', ' ', $promptText);
}
$lines[] = static::tag('option', $promptText, $promptOptions);
} }
$options = isset($tagOptions['options']) ? $tagOptions['options'] : []; $options = isset($tagOptions['options']) ? $tagOptions['options'] : [];

View File

@ -603,6 +603,24 @@ EOD;
], ],
]; ];
$this->assertEqualsWithoutLE(str_replace(' ', ' ', $expected), Html::renderSelectOptions(['value111', 'value1'], $data, $attributes)); $this->assertEqualsWithoutLE(str_replace(' ', ' ', $expected), Html::renderSelectOptions(['value111', 'value1'], $data, $attributes));
// Attributes for prompt (https://github.com/yiisoft/yii2/issues/7420)
$data = [
'value1' => 'label1',
'value2' => 'label2',
];
$expected = <<<EOD
<option class="prompt" value="-1" label="None">Please select</option>
<option value="value1" selected>label1</option>
<option value="value2">label2</option>
EOD;
$attributes = [
'prompt' => [
'text' => 'Please select', 'options' => ['class' => 'prompt', 'value' => '-1', 'label' => 'None'],
],
];
$this->assertEqualsWithoutLE($expected, Html::renderSelectOptions(['value1'], $data, $attributes));
} }
public function testRenderAttributes() public function testRenderAttributes()