Pawan Pawan - 5 months ago 65
PHP Question

Yii2: Adding php sprintf function to format value in gridview

I am trying to add leading zeros to a number to be shown in gridview.
I am trying this code, but it is throwing error like:


Getting unknown property: app\models\OpdTestBill::0000


The code I am using is like this:

[
'attribute'=>'dhanwantri_bill_number',
'format'=>'raw',
'value'=>sprintf('%04d','dhanwantri_bill_number')
],


What is the proper syntax to achieve this?

Thanks.

Variation

[ 'attribute'=>'dhanwantri_bill_number',
'format'=>sprintf('%04d','dhanwantri_bill_number')
],


Here too I am getting the error:


Unknown format type: 0000

throw new InvalidParamException("Unknown format type: $format")

Answer

OK I found the solution, anyone having problem can find it useful.

[
'attribute'=>'dhanwantri_bill_number',
'value'=>function($model) {
 return sprintf('%04d',$model->dhanwantri_bill_number);
            }
],
Comments