Commit 4ad022c3 by Qiang Xue

Fixes #3240: Added support for assigning an anonymous function to…

Fixes #3240: Added support for assigning an anonymous function to `yii\widgets\ActiveForm::fieldConfig`
parent e15cfc02
...@@ -66,6 +66,11 @@ use yii\base\InvalidConfigException; ...@@ -66,6 +66,11 @@ use yii\base\InvalidConfigException;
class ActiveForm extends \yii\widgets\ActiveForm class ActiveForm extends \yii\widgets\ActiveForm
{ {
/** /**
* @var string the default field class name when calling [[field()]] to create a new field.
* @see fieldConfig
*/
public $fieldClass = 'yii\bootstrap\ActiveField';
/**
* @var array HTML attributes for the form tag. Default is `['role' => 'form']`. * @var array HTML attributes for the form tag. Default is `['role' => 'form']`.
*/ */
public $options = ['role' => 'form']; public $options = ['role' => 'form'];
...@@ -91,9 +96,6 @@ class ActiveForm extends \yii\widgets\ActiveForm ...@@ -91,9 +96,6 @@ class ActiveForm extends \yii\widgets\ActiveForm
if ($this->layout !== 'default') { if ($this->layout !== 'default') {
Html::addCssClass($this->options, 'form-' . $this->layout); Html::addCssClass($this->options, 'form-' . $this->layout);
} }
if (!isset($this->fieldConfig['class'])) {
$this->fieldConfig['class'] = ActiveField::className();
}
parent::init(); parent::init();
} }
} }
...@@ -338,7 +338,7 @@ class FixtureController extends \yii\console\controllers\FixtureController ...@@ -338,7 +338,7 @@ class FixtureController extends \yii\console\controllers\FixtureController
} }
/** /**
* Returns array containg fixtures templates file names. You can specify what files to find * Returns array containing fixtures templates file names. You can specify what files to find
* by the given parameter. * by the given parameter.
* @param array $templatesNames template file names to search. If empty then all files will be searched. * @param array $templatesNames template file names to search. If empty then all files will be searched.
* @return array * @return array
...@@ -428,9 +428,9 @@ class FixtureController extends \yii\console\controllers\FixtureController ...@@ -428,9 +428,9 @@ class FixtureController extends \yii\console\controllers\FixtureController
/** /**
* Generates fixture file by the given fixture template file. * Generates fixture file by the given fixture template file.
* @param type $templateName template file name * @param string $templateName template file name
* @param type $templatePath path where templates are stored * @param string $templatePath path where templates are stored
* @param type $fixtureDataPath fixture data path where generated file should be written * @param string $fixtureDataPath fixture data path where generated file should be written
*/ */
public function generateFixtureFile($templateName, $templatePath, $fixtureDataPath) public function generateFixtureFile($templateName, $templatePath, $fixtureDataPath)
{ {
......
...@@ -121,6 +121,7 @@ Yii Framework 2 Change Log ...@@ -121,6 +121,7 @@ Yii Framework 2 Change Log
- Enh #3222: Added `useTablePrefix` option to the model generator for Gii (horizons2) - Enh #3222: Added `useTablePrefix` option to the model generator for Gii (horizons2)
- Enh #3230: Added `yii\filters\AccessControl::user` to support access control with different actors (qiangxue) - Enh #3230: Added `yii\filters\AccessControl::user` to support access control with different actors (qiangxue)
- Enh #3232: Added `export()` and `exportAsString()` methods to `yii\helpers\BaseVarDumper` (klimov-paul) - Enh #3232: Added `export()` and `exportAsString()` methods to `yii\helpers\BaseVarDumper` (klimov-paul)
- Enh #3240: Added support for assigning an anonymous function to `yii\widgets\ActiveForm::fieldConfig` (qiangxue)
- Enh #3244: Allow logging complex data such as arrays and object via the log system (cebe) - Enh #3244: Allow logging complex data such as arrays and object via the log system (cebe)
- Enh #3252: Added support for case insensitive matching using ILIKE to PostgreSQL QueryBuilder (cebe) - Enh #3252: Added support for case insensitive matching using ILIKE to PostgreSQL QueryBuilder (cebe)
- Enh #3280: Support dynamically attaching anonymous behaviors (qiangxue) - Enh #3280: Support dynamically attaching anonymous behaviors (qiangxue)
......
...@@ -51,7 +51,20 @@ class ActiveForm extends Widget ...@@ -51,7 +51,20 @@ class ActiveForm extends Widget
*/ */
public $options = []; public $options = [];
/** /**
* @var array the default configuration used by [[field()]] when creating a new field object. * @var string the default field class name when calling [[field()]] to create a new field.
* @see fieldConfig
*/
public $fieldClass = 'yii\widgets\ActiveField';
/**
* @var array|\Closure the default configuration used by [[field()]] when creating a new field object.
* This can be either a configuration array or an anonymous function returning a configuration array.
* If the latter, the signature should be as follows,
*
* ```php
* function ($model, $attribute)
* ```
*
* @see fieldClass
*/ */
public $fieldConfig; public $fieldConfig;
/** /**
...@@ -223,9 +236,6 @@ class ActiveForm extends Widget ...@@ -223,9 +236,6 @@ class ActiveForm extends Widget
if (!isset($this->options['id'])) { if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId(); $this->options['id'] = $this->getId();
} }
if (!isset($this->fieldConfig['class'])) {
$this->fieldConfig['class'] = ActiveField::className();
}
echo Html::beginForm($this->action, $this->method, $this->options); echo Html::beginForm($this->action, $this->method, $this->options);
} }
...@@ -313,7 +323,14 @@ class ActiveForm extends Widget ...@@ -313,7 +323,14 @@ class ActiveForm extends Widget
*/ */
public function field($model, $attribute, $options = []) public function field($model, $attribute, $options = [])
{ {
return Yii::createObject(array_merge($this->fieldConfig, $options, [ $config = $this->fieldConfig;
if ($config instanceof \Closure) {
$config = call_user_func($config, $model, $attribute);
}
if (!isset($config['class'])) {
$config['class'] = $this->fieldClass;
}
return Yii::createObject(array_merge($config, $options, [
'model' => $model, 'model' => $model,
'attribute' => $attribute, 'attribute' => $attribute,
'form' => $this, 'form' => $this,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment