From 7c85258c36faf286fd03768f2ba00ad951c9ac63 Mon Sep 17 00:00:00 2001 From: Bob van Leeuwen Date: Fri, 5 Aug 2016 21:55:42 +0200 Subject: [PATCH] Fixing issue with filterInputValidation variable in DataColumn close #12089 --- framework/CHANGELOG.md | 2 ++ framework/grid/DataColumn.php | 12 ++++--- tests/framework/grid/DataColumnTest.php | 64 +++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 952dd2f..9bbfafd 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -19,6 +19,8 @@ Yii Framework 2 Change Log - Chg #13080: Rename `yii\base\InvalidParamException` to `yii\base\InvalidArgumentException` (arogachev) - Enh #2990: `yii\widgets\ActiveField::hiddenInput()` no longer renders label by default (lennartvdd) - Chg: Moved masked input field widget into separate extension https://github.com/yiisoft/yii2-maskedinput (samdark) +- Chg #12089: Behavior of `yii\grid\DataColumn::$filterInputOptions` changed when default value is overwritten (bvanleeuwen, cebe) + 2.0.11 under development 2.0.12 under development diff --git a/framework/grid/DataColumn.php b/framework/grid/DataColumn.php index f3749e2..7ef29de 100644 --- a/framework/grid/DataColumn.php +++ b/framework/grid/DataColumn.php @@ -106,9 +106,11 @@ class DataColumn extends Column * @var array the HTML attributes for the filter input fields. This property is used in combination with * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to * render the HTML attributes for the generated filter input fields. + * By default a `'class' => 'form-control'` element will be added if no class has been specified. + * If you do not want to create a class attribute, you can specify `['class' => null]`. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. */ - public $filterInputOptions = ['class' => 'form-control', 'id' => null]; + public $filterInputOptions = []; /** @@ -186,17 +188,19 @@ class DataColumn extends Column } else { $error = ''; } + + $filterOptions = array_merge(['class' => 'form-control', 'id' => null], $this->filterInputOptions); if (is_array($this->filter)) { - $options = array_merge(['prompt' => ''], $this->filterInputOptions); + $options = array_merge(['prompt' => ''], $filterOptions); return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error; } elseif ($this->format === 'boolean') { - $options = array_merge(['prompt' => ''], $this->filterInputOptions); + $options = array_merge(['prompt' => ''], $filterOptions); return Html::activeDropDownList($model, $this->attribute, [ $this->grid->formatter->booleanFormat[0], $this->grid->formatter->booleanFormat[1], ], $options) . $error; } else { - return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error; + return Html::activeTextInput($model, $this->attribute, $filterOptions) . $error; } } else { return parent::renderFilterCellContent(); diff --git a/tests/framework/grid/DataColumnTest.php b/tests/framework/grid/DataColumnTest.php index e9003da..c4f5b1b 100644 --- a/tests/framework/grid/DataColumnTest.php +++ b/tests/framework/grid/DataColumnTest.php @@ -7,6 +7,7 @@ use Yii; use yii\data\ArrayDataProvider; use yii\grid\DataColumn; use yii\grid\GridView; +use yii\i18n\Formatter; use yiiunit\data\ar\ActiveRecord; use yiiunit\data\ar\Order; @@ -193,4 +194,67 @@ HTML HTML , $result); } + + public function filterOptionsProvider() + { + return [ + [ + false, + '', + ], + [ + ['class' => 'form-control'], + '' + ], + [ + ['class' => 'form-control', 'data' => ['value' => "1"]], + '' + ], + [ + ['class' => ''], + '' + ], + [ + ['class' => null], + '' + ], + [ + ['class' => 'form-control', 'id' => 'customer_id'], + '' + ], + [ + ['class' => '', 'id' => 'customer_id'], + '' + ], + [ + ['class' => null, 'id' => 'customer_id'], + '' + ], + ]; + } + + + /** + * @dataProvider filterOptionsProvider + */ + public function testFilterInput_Options($options, $expectedHtml) + { + $this->mockApplication(); + $column = new DataColumn([ + 'attribute' => 'customer_id', + 'grid' => new GridView([ + 'filterModel' => new Order, + 'dataProvider' => new ArrayDataProvider([ + 'allModels' => [], + 'totalCount' => 0, + ]), + ]), + ]); + if ($options !== false) { + $column->filterInputOptions = $options; + } + + $filterCell = $column->renderFilterCell(); + $this->assertEqualsWithoutLE($expectedHtml, $filterCell); + } }