Skip to content

Commit

Permalink
Merge branch 'arogachev-7420-dropdown-prompt-attributes'
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Nov 17, 2016
2 parents bd9e68e + ea251e2 commit c72643b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 38 additions & 7 deletions framework/helpers/BaseHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,13 @@ protected static function booleanInput($type, $name, $checked = false, $options
* 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,
*
Expand Down Expand Up @@ -803,7 +809,13 @@ public static function dropDownList($name, $selection = null, $items = [], $opti
* 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,
*
Expand Down Expand Up @@ -1477,7 +1489,13 @@ protected static function activeBooleanInput($type, $model, $attribute, $options
* 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,
*
Expand Down Expand Up @@ -1526,7 +1544,13 @@ public static function activeDropDownList($model, $attribute, $items, $options =
* 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,
*
Expand Down Expand Up @@ -1693,11 +1717,18 @@ public static function renderSelectOptions($selection, $items, &$tagOptions = []
$encodeSpaces = ArrayHelper::remove($tagOptions, 'encodeSpaces', false);
$encode = ArrayHelper::remove($tagOptions, 'encode', true);
if (isset($tagOptions['prompt'])) {
$prompt = $encode ? static::encode($tagOptions['prompt']) : $tagOptions['prompt'];
$promptOptions = ['value' => ''];
if (is_string($tagOptions['prompt'])) {
$promptText = $tagOptions['prompt'];
} else {
$promptText = $tagOptions['prompt']['text'];
$promptOptions = array_merge($promptOptions, $tagOptions['prompt']['options']);
}
$promptText = $encode ? static::encode($promptText) : $promptText;
if ($encodeSpaces) {
$prompt = str_replace(' ', ' ', $prompt);
$promptText = str_replace(' ', ' ', $promptText);
}
$lines[] = static::tag('option', $prompt, ['value' => '']);
$lines[] = static::tag('option', $promptText, $promptOptions);
}

$options = isset($tagOptions['options']) ? $tagOptions['options'] : [];
Expand Down
18 changes: 18 additions & 0 deletions tests/framework/helpers/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,24 @@ public function testRenderOptions()
],
];
$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()
Expand Down

0 comments on commit c72643b

Please sign in to comment.