kartik-v / yii2-grid

Enhanced GridView with various utilities for Yii Framework 2.0
http://demos.krajee.com/grid
Other
557 stars 302 forks source link

Select2 Filter with Tags cause Array to String exception #949

Closed Lachee closed 4 years ago

Lachee commented 4 years ago

Prerequisites

Steps to reproduce the issue

Creating a multiple tag filter causes the page to throw a array to string conversion exception when any tag is selected. The input normally renders fine, but when trying to filter it throws.

Create a GridView with a filter. Make the filter for an attribute a FILTER_SELECT2 and set it up to use multiple tags, like so:

        [ 
            'attribute' => 'roles',
            'label' => 'Roles',
            'filterType' => kartik\grid\GridView::FILTER_SELECT2,
            'filterWidgetOptions' => [
                'hideSearch' => true,
                'data' => Yii::$app->user->can(Roles::DevOps) ? Roles::getAllRoles() : Roles::getCompanyRoles(),
                'options' => ['placeholder' => 'Select Roles...', 'multiple' => true],
                'pluginOptions' => [
                    'tags' => true,
                    'maximumInputLength' => 10
                ],
            ],
            'value' => function($data) {                
                $roles = Yii::$app->authManager->getRolesByUser($data->id);
                if ($roles == null) return '';
                $label = '';
                foreach($roles as $key => $pair) $label .= (!empty($label) ? ', ' : '') . substr($pair->name, 4);
                return $label;
            }
        ],

For the filter, mark it as an array of strings [['roles'], 'each', 'rule' => ['string' ] ],

Now select a tag in the filter

Expected behavior and actual behavior

When I follow those steps, I see...

yii\base\ErrorException: Array to string conversion in D:\apache\htdocs\project\vendor\yiisoft\yii2\helpers\BaseHtml.php:562
Stack trace:
#0 D:\apache\htdocs\project\vendor\yiisoft\yii2\helpers\BaseHtml.php(562): yii\web\ErrorHandler->handleError()
#1 D:\apache\htdocs\project\vendor\yiisoft\yii2\helpers\BaseHtml.php(1361): yii\helpers\BaseHtml::input()
#2 D:\apache\htdocs\project\vendor\yiisoft\yii2\helpers\BaseHtml.php(1407): yii\helpers\BaseHtml::activeInput()
#3 D:\apache\htdocs\project\vendor\yiisoft\yii2\grid\DataColumn.php(210): yii\helpers\BaseHtml::activeTextInput()
#4 D:\apache\htdocs\project\vendor\kartik-v\yii2-grid\src\DataColumn.php(253): kartik\grid\DataColumn->renderFilterCellContent()
#5 D:\apache\htdocs\project\vendor\yiisoft\yii2\grid\Column.php(119): kartik\grid\DataColumn->renderFilterCellContent()
#6 D:\apache\htdocs\project\vendor\kartik-v\yii2-grid\src\ColumnTrait.php(230): kartik\grid\DataColumn->renderFilterCell()
#7 D:\apache\htdocs\project\vendor\yiisoft\yii2\grid\GridView.php(467): kartik\grid\DataColumn->renderFilterCell()
#8 D:\apache\htdocs\project\vendor\kartik-v\yii2-grid\src\GridView.php(1440): app\widgets\GridView->renderFilters()
#9 D:\apache\htdocs\project\vendor\yiisoft\yii2\grid\GridView.php(357): app\widgets\GridView->renderTableHeader()
#10 D:\apache\htdocs\project\vendor\yiisoft\yii2\widgets\BaseListView.php(160): app\widgets\GridView->renderItems()
#11 D:\apache\htdocs\project\vendor\yiisoft\yii2\grid\GridView.php(326): app\widgets\GridView->renderSection()
#12 D:\apache\htdocs\project\vendor\yiisoft\yii2\widgets\BaseListView.php(135): app\widgets\GridView->renderSection()
#13 D:\apache\htdocs\project\vendor\yiisoft\yii2\widgets\BaseListView.php(138): app\widgets\GridView->yii\widgets\{closure}()
#14 D:\apache\htdocs\project\vendor\yiisoft\yii2\widgets\BaseListView.php(138): ::preg_replace_callback()
#15 D:\apache\htdocs\project\vendor\yiisoft\yii2\grid\GridView.php(301): app\widgets\GridView->run()
#16 D:\apache\htdocs\project\vendor\kartik-v\yii2-grid\src\GridView.php(1207): app\widgets\GridView->run()
#17 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\Widget.php(140): app\widgets\GridView->run()
#18 D:\apache\htdocs\project\views\user\manage.php(104): yii\base\Widget::widget()
#19 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\View.php(348): yii\web\View->unknown()
#20 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\View.php(257): yii\web\View->renderPhpFile()
#21 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\View.php(156): yii\web\View->renderFile()
#22 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\Controller.php(386): yii\web\View->render()
#23 D:\apache\htdocs\project\controllers\UserController.php(86): app\controllers\UserController->render()
#24 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\InlineAction.php(57): app\controllers\UserController->actionManage()
#25 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\InlineAction.php(57): ::call_user_func_array:{D:\apache\htdocs\project\vendor\yiisoft\yii2\base\InlineAction.php:57}()
#26 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams()
#27 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\Module.php(528): app\controllers\UserController->runAction()
#28 D:\apache\htdocs\project\vendor\yiisoft\yii2\web\Application.php(103): yii\web\Application->runAction()
#29 D:\apache\htdocs\project\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest()
#30 D:\apache\htdocs\project\public\index.php(16): yii\web\Application->run()
#31 {main}

I was expecting... The tag to be selected in the filter of the grid and a array of strings given to the filter model.

Environment

Windows 10
Apache/2.4.33 (Win64) PHP/7.2.5

Browsers

its a backend issue

Operating System

Libraries

Isolating the problem

Lachee commented 4 years ago

I still have this issue with version dev-master 9f8773367d

Lachee commented 4 years ago

This issue is critical in my application for effective filtering of user roles. Please advise.

Lachee commented 4 years ago

This seems to have been fixed. Unclear what the cause was still.