如何在yii2的radioList()中添加不同的和共同的类

How to add different and common classes in radioList() in yii2

这是我的一个更新表单中单选按钮的 chtml。

 <?= $form->field($model, 'customer_sel')->radioList(array('customer' => 'Customer', 'supplier' => 'Supplier', 'excludecustomer' => 'Exclude Customer', 'all' => 'All'))->label('Select Choice'); ?>

我需要为每个无线电输入添加 class 个常见(相同)和不同 class 个名称。 那就是 html...

中的样子
 <div class="form-group field-cashbankentry-customer_sel">
<label class="control-label" for="cashbankentry-customer_sel">Select Choice</label>
<input type="hidden" name="CashBankentry[customer_sel]" value=""><div id="cashbankentry-customer_sel"><label><input type="radio" class="inputbox inputindex_1" name="CashBankentry[customer_sel]" value="customer" checked> Custom</label>
<label><input type="radio" class="inputbox inputindex_2 name="CashBankentry[customer_sel]" value="supplier"> Supplier</label>
<label><input type="radio" class="inputbox inputindex_3 name="CashBankentry[customer_sel]" value="excludecustomer"> Exclude Customer</label>
<label><input type="radio" class="inputbox inputindex_4 name="CashBankentry[customer_sel]" value="all"> All</label></div>

我有 class 可以添加 [输入框(所有人通用)和 inputindex_1-4 到每个收音机] 我在语法上对向其中添加 classes 数组感到困惑。 如何将这些 class 添加到此

<?= $form->field($model, 'customer_sel')->radioList(array('customer' => 'Customer', 'supplier' => 'Supplier', 'excludecustomer' => 'Exclude Customer', 'all' => 'All'))->label('Select Choice'); ?>

我认为如果不使用 $form->field() 帮助程序生成标记会更容易。检查它的实现,你会看到你需要分开编码的不同部分。

$form->field()的调用基本上是对Html::activeLabel()Html::activeInput()Html::activeError()

的调用

需要为radioList选项配置item选项,详见Html::activeRadioList()

下面的代码应该适合你。

<?php echo $form->field($model, 'customer_sel')->radioList(
        [
            'customer' => 'Customer',
            'supplier' => 'Supplier',
            'excludecustomer' => 'Exclude Customer',
            'all' => 'All',
        ],
        [
            'item' => function ($index, $label, $name, $checked, $value) {

                $return = '<label>';
                $return .= '<input class="inputbox inputindex_' . (++$index) . '" type="radio" name="' . $name . '" value="' . $value . '" ' . ($checked ? "checked" : "") . '>';
                $return .= ucwords($label);
                $return .= '</label>';
                return $return;
            },

        ]
    )->label('Select Choice');
?>

注意:要使第一个选项默认选中,您应该在操作中设置带有值的属性。喜欢

$model = new CashBankentry();
$model->customer_sel='customer';