From ea251e24baec890ab2ba87987807217e1938eead Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Wed, 16 Nov 2016 23:49:02 +0600 Subject: [PATCH] Closes #7420: Attributes for prompt generated with `renderSelectOptions` of `\yii\helpers\Html` helper --- framework/CHANGELOG.md | 1 + framework/helpers/BaseHtml.php | 47 +++++++++++++++++++++++----- tests/framework/helpers/HtmlTest.php | 18 +++++++++++ 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 2e8dc58edd..8e2a468b3f 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -22,6 +22,7 @@ Yii Framework 2 Change Log - 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) - 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 #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) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index ebbc862594..a8ce79769e 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -754,7 +754,13 @@ class BaseHtml * 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: * - * - 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, * 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. * @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, * 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. * @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, * 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. * @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, * 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); $encode = ArrayHelper::remove($tagOptions, 'encode', true); if (isset($tagOptions['prompt'])) { - $prompt = $encode ? static::encode($tagOptions['prompt']) : $tagOptions['prompt']; - if ($encodeSpaces) { - $prompt = str_replace(' ', ' ', $prompt); + $promptOptions = ['value' => '']; + if (is_string($tagOptions['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'] : []; diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php index 0c23701997..969e2971d9 100644 --- a/tests/framework/helpers/HtmlTest.php +++ b/tests/framework/helpers/HtmlTest.php @@ -603,6 +603,24 @@ EOD; ], ]; $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 = <<Please select + + +EOD; + $attributes = [ + 'prompt' => [ + 'text' => 'Please select', 'options' => ['class' => 'prompt', 'value' => '-1', 'label' => 'None'], + ], + ]; + $this->assertEqualsWithoutLE($expected, Html::renderSelectOptions(['value1'], $data, $attributes)); } public function testRenderAttributes()