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;
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']`.
*/
public $options = ['role' => 'form'];
......@@ -91,9 +96,6 @@ class ActiveForm extends \yii\widgets\ActiveForm
if ($this->layout !== 'default') {
Html::addCssClass($this->options, 'form-' . $this->layout);
}
if (!isset($this->fieldConfig['class'])) {
$this->fieldConfig['class'] = ActiveField::className();
}
parent::init();
}
}
......@@ -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.
* @param array $templatesNames template file names to search. If empty then all files will be searched.
* @return array
......@@ -428,9 +428,9 @@ class FixtureController extends \yii\console\controllers\FixtureController
/**
* Generates fixture file by the given fixture template file.
* @param type $templateName template file name
* @param type $templatePath path where templates are stored
* @param type $fixtureDataPath fixture data path where generated file should be written
* @param string $templateName template file name
* @param string $templatePath path where templates are stored
* @param string $fixtureDataPath fixture data path where generated file should be written
*/
public function generateFixtureFile($templateName, $templatePath, $fixtureDataPath)
{
......
......@@ -121,6 +121,7 @@ Yii Framework 2 Change Log
- 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 #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 #3252: Added support for case insensitive matching using ILIKE to PostgreSQL QueryBuilder (cebe)
- Enh #3280: Support dynamically attaching anonymous behaviors (qiangxue)
......
......@@ -51,7 +51,20 @@ class ActiveForm extends Widget
*/
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;
/**
......@@ -223,9 +236,6 @@ class ActiveForm extends Widget
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
if (!isset($this->fieldConfig['class'])) {
$this->fieldConfig['class'] = ActiveField::className();
}
echo Html::beginForm($this->action, $this->method, $this->options);
}
......@@ -313,7 +323,14 @@ class ActiveForm extends Widget
*/
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,
'attribute' => $attribute,
'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