Search code examples
phpyii2radiobuttonlistyii2-basic-app

Display Image With Label In Radiobutton - Yii2


Due to some requirement, I need to show Image with label in radio button list.

I searched How to insert an Image List into a radioButtonList?. And, incorporated the changes.

View

<?php
$model->pay_type = 1;
echo $form->field($model, 'pay_type')->radioList($model->paymentOptionRadioButtonList())->label(false);
?>

Model

<?php
public function paymentOptionRadioButtonList(){
  $payment_options = $params->payment_type;

  foreach($payment_options as $key => $val){
    $payment_options[$key] = $val.yii\helpers\Html::img('www.dummy.com/'.'company-default-logo.jpg');
  }
  return $payment_options;
}

?>

Output:

enter image description here

Instead of showing image, It's showing <img src="www.dummy.com/company-default-logo.jpg" alt="">

Is there any parameters I need to pass in radio button list to show image?


Solution

  • You should disable encode option for radioList to show raw HTML. e.g.

    echo $form->field($model, 'pay_type')->radioList($model->paymentOptionRadioButtonList(), ['encode' => false])->label(false);
    

    More info here: http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#radioList()-detail