I get an error when I use a function to get the value of an attribute and it's working normally using Gridview. What I'm doing wrong?
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => function ($data) {
return Lookup::item("SubjectType", $data->subject_type);
},
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>
I have experienced this kind of problem. The error was
PHP Warning – yii\base\ErrorException
htmlspecialchars() expects parameter 1 to be string, object given
what I did was transfer the function in the model like this.
function functionName($data) {
return Lookup::item("SubjectType", $data->subject_type);
},
and then in your view.php file..
[
'label'=>'subject_type',
'value'=>$model->functionName($data),
],
Just use call_user_func()
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => call_user_func(function ($data) {
return Lookup::item("SubjectType", $data->subject_type);
}, $model),
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>
Fabrizio Caldarelli, on 05 January 2015 - 03:53 PM, said: Yes because 'value' attribute is a real value or attribute name, as doc says
So your code should be:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => Lookup::item("SubjectType", $model->subject_type),
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With