Yii2 htmlspecialchars()期望参数1是字符串,给定对象

时间:2016-02-12 19:10:43

标签: yii yii2

我正在使用 DetailView 小部件,我正在尝试将自定义函数编写为值,但我一直收到此错误:

atoll

这是代码:

htmlspecialchars() expects parameter 1 to be string, object given

我做错了什么?

2 个答案:

答案 0 :(得分:2)

In DetailView value don't accept anonymous function (differently respect grdiView) .. in this case you can set the related value using a call to a normal function .. remember that in DetailView you work directly to $model and not on a $dataProvider content

then you can use a function

function myFunction($model, $key, $inde){

                if(!empty($model->keys))
                    return '<strong>User don\'t have any key at the moment.</strong>';

                $keys = '';
                foreach($model->keys as $key):

                    $keys .= '<strong>' . $key->key . '</strong>';

                endforeach;

                return $keys;
   }

or simple assign the result to a var

and in detail view attribute call the function or assigne the var directly

 [
            'label' => 'Keys',
            'value' => myFunction($model, $key, $inde),

答案 1 :(得分:1)

添加'format' => 'html'

[
            'label' => 'Keys',
            'format' => 'html',
            'value' => function($model, $key, $inde){

                if(!empty($model->keys))
                    return '<strong>User don\'t have any key at the moment.</strong>';

                $keys = '';
                foreach($model->keys as $key):

                    $keys .= '<strong>' . $key->key . '</strong>';

                endforeach;

                return $keys;

            }
],