Browse Source

Added `yii\widgets\InputWidget::$field` field, allowing access to the related `yii\widget\ActiveField` instance

tags/2.0.11
Klimov Paul 8 years ago
parent
commit
3946ac0dab
  1. 1
      framework/CHANGELOG.md
  2. 10
      framework/widgets/ActiveField.php
  3. 6
      framework/widgets/InputWidget.php
  4. 39
      tests/framework/widgets/ActiveFieldTest.php

1
framework/CHANGELOG.md

@ -88,6 +88,7 @@ Yii Framework 2 Change Log
- Enh #13074: Improved `yii\log\SyslogTarget` with `$options` to be able to change the default `openlog` options (timbeks)
- Enh #13122: Optimized query for information about foreign keys in `yii\db\oci` (zlakomanoff)
- Enh #13202: Refactor validateAttribute method in UniqueValidator (developeruz)
- Enh #13264: Added `yii\widgets\InputWidget::$field` field, allowing access to the related `yii\widget\ActiveField` instance (klimov-paul)
- Enh #13268: Added logging of memory usage (bashkarev)
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
- Enh: Refactored `yii\web\ErrorAction` to make it reusable (silverfire)

10
framework/widgets/ActiveField.php

@ -713,10 +713,14 @@ class ActiveField extends Component
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
if (isset($config['options']) && isset(class_parents($class)['yii\widgets\InputWidget'])) {
$this->addAriaAttributes($config['options']);
$this->adjustLabelFor($config['options']);
if (isset(class_parents($class)['yii\widgets\InputWidget'])) {
$config['field'] = $this;
if (isset($config['options'])) {
$this->addAriaAttributes($config['options']);
$this->adjustLabelFor($config['options']);
}
}
$this->parts['{input}'] = $class::widget($config);
return $this;

6
framework/widgets/InputWidget.php

@ -37,6 +37,12 @@ use yii\helpers\Html;
class InputWidget extends Widget
{
/**
* @var \yii\widgets\ActiveField active input field, which triggers this widget rendering.
* This field will be automatically filled up in case widget instance is created via [[\yii\widgets\ActiveField::widget()]].
* @since 2.0.11
*/
public $field;
/**
* @var Model the data model that this widget is associated with.
*/
public $model;

39
tests/framework/widgets/ActiveFieldTest.php

@ -8,6 +8,7 @@ use yii\base\DynamicModel;
use yii\widgets\ActiveForm;
use yii\web\View;
use yii\web\AssetManager;
use yii\widgets\InputWidget;
/**
* @author Nelson J Morais <njmorais@gmail.com>
@ -147,7 +148,8 @@ EOD;
$this->assertEquals($expectedValue, $actualValue);
}
public function testBegin() {
public function testBegin()
{
$expectedValue = '<article class="form-group field-activefieldtestmodel-attributename">';
$this->activeField->options['tag'] = 'article';
$actualValue = $this->activeField->begin();
@ -217,7 +219,6 @@ EOT;
$this->assertEquals($expectedValue, $this->activeField->parts['{label}']);
}
public function testError()
{
$expectedValue = '<label class="control-label" for="activefieldtestmodel-attributename">Attribute Name</label>';
@ -505,6 +506,21 @@ EOD;
$this->assertEqualsWithoutLE($expectedValue, trim($actualValue));
}
public function testWidget()
{
$this->activeField->widget(TestInputWidget::className());
$this->assertEquals('Render: ' . TestInputWidget::className(), $this->activeField->parts['{input}']);
$widget = TestInputWidget::$lastInstance;
$this->assertSame($this->activeField->model, $widget->model);
$this->assertEquals($this->activeField->attribute, $widget->attribute);
$this->assertSame($this->activeField->form->view, $widget->view);
$this->assertSame($this->activeField, $widget->field);
$this->activeField->widget(TestInputWidget::className(), ['options' => ['id' => 'test-id']]);
$this->assertEquals('test-id', $this->activeField->labelOptions['for']);
}
/**
* Helper methods
*/
@ -571,3 +587,22 @@ class TestValidator extends \yii\validators\Validator
$this->whenClient = $js;
}
}
class TestInputWidget extends InputWidget
{
/**
* @var static
*/
public static $lastInstance;
public function init()
{
parent::init();
self::$lastInstance = $this;
}
public function run()
{
return 'Render: ' . get_class($this);
}
}
Loading…
Cancel
Save