Yii2: Html::radioList improvement

Created on 28 Oct 2014  路  12Comments  路  Source: yiisoft/yii2

Is this possible to make set of the some values disabled?

Most helpful comment

You'll have to override the code that generates each list item by injecting a callback function into the options array.

Here's a working example of what you're trying to achieve:

use yii\helpers\Html;

$name = "radiotest";
$items = [1 => "A", 2 => "B", 3 => "C", 4 => "D", 5 => "E"];
$selection = 2;

echo Html::radioList($name, $selection, $items, [
    'item' => function ($index, $label, $name, $checked, $value) {
        $disabled = true; // replace with whatever check you use for each item
        return Html::radio($name, $checked, [
            'value' => $value,
            'label' => Html::encode($label),
            'disabled' => $disabled,
        ]);
    },
]);

All 12 comments

What do you mean?

We have $selection param in the radioList function. It allows to set some buttons as checked. What if some of the list's buttons should be disabled?

Sure, i can use Html::radio() in the loop.

@mdmunir not sure what do you mean. Yes, I mean this function. But I can't set some of the buttons in the list as disabled.

You'll have to override the code that generates each list item by injecting a callback function into the options array.

Here's a working example of what you're trying to achieve:

use yii\helpers\Html;

$name = "radiotest";
$items = [1 => "A", 2 => "B", 3 => "C", 4 => "D", 5 => "E"];
$selection = 2;

echo Html::radioList($name, $selection, $items, [
    'item' => function ($index, $label, $name, $checked, $value) {
        $disabled = true; // replace with whatever check you use for each item
        return Html::radio($name, $checked, [
            'value' => $value,
            'label' => Html::encode($label),
            'disabled' => $disabled,
        ]);
    },
]);

Use item option as suggested by @lindelius (thanks to @lindelius for the help)

All clear. Thanks!

No worries! :)

<?= $form->field($model, 'command')->radioList($model->getCommands(), ['itemOptions' => ['disabled' => true]]) ?>
See source code \yii\helpers\BaseHtml::radioList()

@dimka3210 ['itemOptions' => ['disabled' => true]] will disable all radio buttons in a radio list, if one just want to disable a subset of the radio list, the item option would be better.

@drodata, yes. I inattentively read topic.

@dimka3210 ['itemOptions' => ['disabled' => true]] will disable all radio buttons in a radio list, if one just want to disable a subset of the radio list, the item option would be better.

How to disable item.

Was this page helpful?
0 / 5 - 0 ratings