Skip to content

Commit

Permalink
Fixes #16681: ActiveField::inputOptions were not used during some w…
Browse files Browse the repository at this point in the history
…idgets rendering
  • Loading branch information
np25071984 authored and samdark committed Mar 9, 2019
1 parent 5b5150a commit a8d4f85
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.17 under development
------------------------

- Bug #16681: `ActiveField::inputOptions` were not used during some widgets rendering (GHopperMSK)
- Bug #17133: Fixed aliases rendering during help generation for a console command (GHopperMSK)
- Bug #17185: Fixed `AssetManager` timestamp appending when a file is published manually (GHopperMSK)
- Bug #17156: Fixes PHP 7.2 warning when a data provider has no data as a parameter for a GridView (evilito)
Expand Down
5 changes: 5 additions & 0 deletions framework/widgets/ActiveField.php
Expand Up @@ -763,6 +763,11 @@ public function radioList($items, $options = [])
*/
public function widget($class, $config = [])
{
foreach ($this->inputOptions as $key => $value) {
if (!isset($config['options'][$key])) {
$config['options'][$key] = $value;
}
}
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
Expand Down
53 changes: 53 additions & 0 deletions tests/framework/widgets/ActiveFieldTest.php
Expand Up @@ -14,6 +14,7 @@
use yii\widgets\ActiveField;
use yii\widgets\ActiveForm;
use yii\widgets\InputWidget;
use yii\widgets\MaskedInput;

/**
* @author Nelson J Morais <njmorais@gmail.com>
Expand Down Expand Up @@ -584,6 +585,30 @@ public function testOptionsClass()
$this->assertEqualsWithoutLE($expectedValue, trim($actualValue));
}

public function testInputOptionsTransferToWidget()
{
$widget = $this->activeField->widget(TestMaskedInput::className(), [
'mask' => '999-999-9999',
'options' => ['placeholder' => 'pholder_direct'],
]);
$this->assertContains('placeholder="pholder_direct"', (string) $widget);

// transfer options from ActiveField to widget
$this->activeField->inputOptions = ['placeholder' => 'pholder_input'];
$widget = $this->activeField->widget(TestMaskedInput::className(), [
'mask' => '999-999-9999',
]);
$this->assertContains('placeholder="pholder_input"', (string) $widget);

// set both AF and widget options (second one takes precedence)
$this->activeField->inputOptions = ['placeholder' => 'pholder_both_input'];
$widget = $this->activeField->widget(TestMaskedInput::className(), [
'mask' => '999-999-9999',
'options' => ['placeholder' => 'pholder_both_direct']
]);
$this->assertContains('placeholder="pholder_both_direct"', (string) $widget);
}

/**
* Helper methods.
*/
Expand Down Expand Up @@ -667,3 +692,31 @@ public function run()
return 'Render: ' . get_class($this);
}
}

class TestMaskedInput extends MaskedInput
{
/**
* @var static
*/
public static $lastInstance;

public function init()
{
parent::init();
self::$lastInstance = $this;
}

public function getOptions() {
return $this->options;
}

public function run()
{
return 'Options: ' . implode(', ', array_map(
function ($v, $k) { return sprintf('%s="%s"', $k, $v); },
$this->options,
array_keys($this->options)
));
}
}

0 comments on commit a8d4f85

Please sign in to comment.