2

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")

2 Answers 2

1

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);
    }
],
Sign up to request clarification or add additional context in comments.

Comments

0

You can find the documentation of sptrinf here.

The first parameter is the format that is being used for the output, which you chose to be an integer with leading zeros in case the value is < 1000. The following parameters have to match the format.

sprintf('%04d','dhanwantri_bill_number')

You set an integer as output but gave a string as input. sprintf doesn't know to do with that, since it was looking for an integer.

sprintf('%04d',12);
// Output:
0012

1 Comment

Thanks moonlight - I have updated the question with the variation you suggested. I understand the php sprintf function, only the correct syntax for using it in Yii framework is causing the headache. I am giving the field attribute, how I can replace that can please suggest that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.