Search code examples
yii2yii2-advanced-appdetailview

How to use a condition inside Detail view widget in Yii2?


following is a detail view widget

 <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'name',
            'entity_name',
            'voucher_category',
            'credit',
            'debit',
            'remarks:ntext',
            'posting_date',
            'payment.method',

            [
            'label'  => 'Reference Date',
            'value' => $model->reference_date !=NULL  ?  $model->reference_date: 'Not Defined',
            ],
            'voucher_no',

        ],
    ]) ?>

what i want is to check that

                    if($model->voucher_category ==0)
                    {
                        return "Income Voucher";
                    }
                    elseif($model->voucher_category ==1)
                    {
                        return "Exepense Voucher";
                    }
                    else
                    {
                         return "General Voucher"; 
                    }

ie, i want to check a condition based on which a value should be displayed in the view. How can i do this in a detail view widget?


Solution

  • You can add condition using ternary. For Example,

    [
     'attribute' => 'voucher_category',
     'value' => (($model->voucher_category ==0) ? "Income Voucher": (($model->voucher_category ==1)? "Exepense Voucher" : "General Voucher")),
    ],