本文是使用ActiveForm的笔记
单选框:radio(),radioList();
把html用ActiveForm渲染生成radio,用法如下
转换前的html代码
<label><input type="radio" class="ace" checked="checked"><span class="lbl">保密</span></label>
<label><input type="radio" class="ace">
<span class="lbl">男</span></label>
<label><input type="radio" class="ace">
<span class="lbl">女</span></label>
使用radioList方法
<?php
$items = ['0'=>'保密','1'=>'男','2'=>'女'];
$options = [
'separator'=>' ',
'item' => function($index, $label, $name, $checked, $value) {
$checked=$checked?"checked":"";
return $return = '<label><input name="form-field-radio" type="radio" value="' . $value . '" class="ace" '. $checked .'> <span class="lbl">'.$label.'</span></label>';
}
];
echo $form->field($model,'sex')->radioList($items,$options)->label('')?>
在Model可以设置
public $sex = 0; //视图中性别默认为保密
上面是radiobox结构比较复杂的情况,所以要使用options的item配置一个匿名函数,用于构建radio,至于简单的结构,直接使用以下
<?= $form->field($model,'sex')->radioList(['0'=>'保密'])?>
更多关于RadioList的参数说明,请到yii-bootstrap-activefield.html#radioList()-detail
以及radio()-detail的参数说明
后面的用到待更新