yii2.0 Activeform表单部分组件使用方法
ActiveInput / ActiveDropDownList
PHP Code复制内容到剪贴板
- use yii\widgets\ActiveForm;
- $form = ActiveForm::begin([
- 'layout' => 'horizontal', // default / horizontal
- 'fieldConfig' => [
- 'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
- 'horizontalCssClasses' => [
- 'label' => 'col-sm-4',
- 'offset' => 'col-sm-offset-4',
- 'wrapper' => 'col-sm-8',
- 'error' => '',
- 'hint' => '',
- ],
- ],
- ]);
- ActiveForm::end();
PHP Code复制内容到剪贴板
- 文本框:textInput();
- 密码框:passwordInput();
- 单选框:radio(),radioList();
- 复选框:checkbox(),checkboxList();
- 下拉框:dropDownList();
- 隐藏域:hiddenInput();
- 文本域:textarea(['rows'=>3]);
- 文件上传:fileInput();
- 提交按钮:submitButton();
- 重置按钮:resetButtun();
获取当前model的所有属性:
PHP Code复制内容到剪贴板
- $model->attributes()
$model->getAttribute("name") //获取值
Html::getInputName($model, 'unzip') // 获取根据active表单生成的name值
$model->getAttributeLabel("password") // 获取指定model下指定字段的label字符串
$model->isAttributeRequired("username") // 获取是否为必填项
use yii\widgets\ActiveForm;
use yii\helpers\Html;
PHP Code复制内容到剪贴板
- <?php
- $form = ActiveForm::begin(['action' => ['test/getpost'],'method'=>'post',]); ?>
- <? echo $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>
- <? echo $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?>
- <? echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?>
- <? echo $form->field($model, 'edu')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px']) ?>
- <? echo $form->field($model, 'file')->fileInput() ?>
- <? echo $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>
- <? echo $form->field($model, 'info')->textarea(['rows'=>3]) ?>
- <? echo $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?>
- <? echo Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
- <? echo Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
- <?php ActiveForm::end(); ?>
隐藏label:
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'plan_id')->hiddenInput(['value' => $model->plan_id?$model->plan_id:\Yii::$app->request->get("id")])->label(false); ?>
隐藏文本框:
PHP Code复制内容到剪贴板
- <?= Html::hiddenInput("input-name", "3") ?>
给input替换class名称:
XML/HTML Code复制内容到剪贴板
- <?= $form->field($model, 'content',[
- 'inputOptions' => ['class'=>'comment_content']
- ])->textInput()->label(false) ?>
select 给默认值 ,并禁用,不允许选择:(默认值 在controller里面给如:$model->type=1)
PHP Code复制内容到剪贴板
- <?=
- $form->field($model, 'participate_type')
- ->radioList(Plan::getPlanParticipateType(),
- ['unselect'=>null,'itemOptions'=>['disabled' => 'disabled']]
- )
- ->label(false)
- ?>
阻止表单提交/表单提交之前动作
PHP Code复制内容到剪贴板
- <?php $form = ActiveForm::begin(['action' => Url::to(['/comment/create']),'fieldConfig' => [ 'template' => "{label}{input}"],'options' => ['data-pjax' => true,'id'=>'create_comment','onsubmit' => 'javascript:return false;'] ]); ?>
YII2 activeform样式修改
PHP Code复制内容到剪贴板
- <?php $form = ActiveForm::begin([
- 'options' => [
- 'class' => '',
- 'data-ajax' => '1',
- 'data-callback' => 'window.location.href=SITE_URL',
- ],
- 'fieldConfig' => [
- 'template' => "<div class='ui-form-item ui-form-item-show ui-border-b'>{label}{input}</div>{error}",
- ],
- ]); ?>
- <?= $form->field($model, 'email') ?>
- <?= $form->field($model, 'phone') ?>
- <?= $form->field($model, 'password')->passwordInput() ?>
- <?= $form->field($model, 're_password')->passwordInput() ?>
- <div class="ui-btn-wrap">
- <?= Html::submitButton('注册', ['class' => 'ui-btn-lg ui-btn-primary']) ?>
- </div>
- <?php ActiveForm::end(); ?>
PHP Code复制内容到剪贴板
- <?php $form = ActiveForm::begin([
- 'options'=>['class' => 'form-horizontal'],
- 'fieldConfig' => [
- 'template' => '{label}<div class="col-sm-4">{input}</div><div class="col-sm-5">{error}</div>',
- 'labelOptions' => ['class' => 'col-sm-2 control-label'],
- ],
- ]); ?>
- <!--正常表单-->
- <?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
- <?= $form->field($model, 'phone')->textInput(['maxlength' => true]) ?>
- <?= $form->field($model, 'company_name')->textInput(['maxlength' => true]) ?>
- <?= $form->field($model, 'need')->textarea(['rows' => 6]) ?>
- <?= $form->field($model, 'title')->textInput(['maxlength' => 255,'placeholder'=>'请输入专题名称'])->label('专题名称') ?>
- <?= $form->field($model, 'img_path',[
- 'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-2\"><label for=\"uploadImg\" class=\"btn btn-default forfocus1\" onclick=\"changeImg(1,'forfocus');\">选择图片<img src=\"\"></label></div>\n{error}",
- 'labelOptions' => ['class' => 'col-lg-2 control-label'],
- ])->textInput(['id'=>'img_path','maxlength' => 255,'placeholder'=>'专题图片','readonly'=>true])->label('专题名称') ?>
- <?= $form->field($model, 'showImg',[
- 'template' => "{label}\n<div id=\"showImg\" class='col-lg-10'></div>",
- 'labelOptions' => ['class' => 'col-lg-2 control-label'],
- ])->label('图片预览') ?>
- <?= $form->field($model, 'link_to')->textInput(['maxlength' => 255,'placeholder'=>'请输入专题链接'])->label('专题链接') ?>
- </div>
- <div class="form-group" style="margin-left: 50px;">
- <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
- </div>
- <?php ActiveForm::end(); ?>
activeForm生成的表单中,每行field都会加一个div:<div class='form-group'>,
想让它不生成的选项:
PHP Code复制内容到剪贴板
- <?php $form = ActiveForm::begin([
- 'id' => 'form-signup',
- 'options' => [
- 'class' => 'layui-form',
- ],
- 'fieldConfig' => [
- 'options' => [
- 'tag' => false, //不生成上一层的<div class='form-group'>
- ],
- ],
- ]); ?>
如果有错误提示的时候,div会加一个class:has-error ,想让它改一个css名称:
PHP Code复制内容到剪贴板
- <?php $form = ActiveForm::begin([
- 'errorCssClass' => 'error'
- ]); ?>
替换掉错误提示的div的样式,替换掉help-block:
PHP Code复制内容到剪贴板
- <?php $form = ActiveForm::begin([
- 'fieldConfig' => [
- 'errorOptions' => [
- 'class' => 'layui-form-mid layui-word-aux', //替换掉help-block
- ]
- ],
- ]); ?>
搜索,一行展示:
PHP Code复制内容到剪贴板
- <?php
- Yii::$container->set(\yii\widgets\ActiveField::className(), ['template' => "{label}\n{input}"]);
- $form = ActiveForm::begin([
- 'action' => ['index'],
- 'method' => 'get',
- 'options' => ['class' => 'form-inline'],
- ]); ?>
加自定义模板:
PHP Code复制内容到剪贴板
- <?=
- $form->field($model, 'participate_type')
- ->radioList(
- Plan::getPlanParticipateType(),
- [
- 'unselect'=>null,
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : "";
- $template = '<input type="radio" ' . $label . ' name="' . $name . '" value="' . $value . '" title="' . $label . '" ' . $c . ' disabled>';
- return $template;
- }
- ]
- )
- ->label(false)
- ?>
PHP Code复制内容到剪贴板
- <?=
- $form->field($model, 'participate_type')
- ->radioList(
- Plan::getPlanParticipateType(),
- [
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : "";
- $template = '<input type="radio" ' . $label . ' name="' . $name . '" value="' . $value . '" title="' . $label . '" ' . $c . '>';
- return $template;
- }
- ]
- )
- ?>
PHP Code复制内容到剪贴板
- <?php
- $labelOptions = [
- 'class' => 'control-label'
- ];
- $inputOptions = [
- 'class' => 'form-control'
- ];
- ?>
- <div class="form-group">
- <?= Html::activeLabel($model, 'meta_title', $labelOptions)?>
- <?= Html::activeTextInput($model, 'meta_title', $inputOptions)?>
- </div>
- <div class="form-group">
- <?= Html::activeLabel($model, 'meta_keyword', $labelOptions)?>
- <?= Html::activeTextInput($model, 'meta_keyword', $inputOptions)?>
- </div>
- <div class="form-group">
- <?= Html::activeLabel($model, 'meta_description', $labelOptions)?>
- <?= Html::activeTextarea($model, 'meta_description', $inputOptions)?>
- </div>
PHP Code复制内容到剪贴板
- <div class="box box-primary">
- <div class="box-body">
- <?php $form = ActiveForm::begin([
- 'action' => ['index'],
- 'method' => 'get',
- ]); ?>
- <div class="row">
- <div class="col-md-3">
- <?= Html::activeDropDownList($model, 'user_name', User::getDropDownListUsers(), ['class' => 'form-control','placeholder' => '请输入标题名称','prompt'=>'请选择']) ?>
- </div>
- <div class="col-md-3">
- <?= Html::activeDropDownList($model, 'published', ['0' => '未发布','1' => '已发布'], ['class' => 'form-control','placeholder' => '请输入标题名称','prompt'=>'请选择'])?>
- </div>
- <div class="col-md-3"><?= Html::activeTextInput($model, 'title', ['class' => 'form-control','placeholder' => '请输入标题名称'])?></div>
- <div class="col-md-3"></div>
- </div>
- <div class="form-group text-center content-header">
- <?= Html::submitButton('Search', ['class' => 'btn btn-primary btn-flat']) ?>
- <?= Html::resetButton('Reset', ['class' => 'btn btn-default btn-flat']) ?>
- </div>
- <?php ActiveForm::end(); ?>
- </div>
- </div>
PHP Code复制内容到剪贴板
- <div class="layui-form-item required">
- <label class="layui-form-label control-label">房屋楼层</label>
- <div class="layui-input-block">
- <div class="layui-inline">
- <label class="layui-form-label" style="width: 30px; padding:9px 10px;">第</label>
- <div class="layui-input-inline" style="width: 60px">
- <?= Html::activeTextInput($moduleModel, 'house_floor', [
- 'class' => 'layui-input',
- 'lay-verify' => 'required',
- 'autocomplete' => 'off'
- ]) ?>
- </div>
- <label class="layui-form-label" style="width: 20px; padding:9px 0px;">层</label>
- </div>
- <div class="layui-inline">
- <label class="layui-form-label" style="width: 30px; padding:9px 10px;">共</label>
- <div class="layui-input-inline" style="width: 60px">
- <?= Html::activeTextInput($moduleModel, 'house_zonglouceng', [
- 'class' => 'layui-input',
- 'size' => 5
- ]) ?>
- </div>
- <label class="layui-form-label" style="width: 20px; padding:9px 0px;">层</label>
- </div>
- </div>
- </div>
PHP Code复制内容到剪贴板
- <div class="layui-form-item required">
- <label class="layui-form-label control-label">房屋户型</label>
- <div class="layui-input-block">
- <div class="layui-inline">
- <div class="layui-input-inline">
- <!-- --><?//= Html::activeTextInput($moduleModel, 'house_shi', [
- // 'class' => 'layui-input',
- // 'required' => '',
- // 'lay-verify' => 'required|number',
- // 'autocomplete' => 'off'
- // ]) ?>
- <?= Html::activeDropDownList($moduleModel, 'house_shi', $moduleModel->getHouseRoom(), [
- 'class' => 'layui-input',
- ]) ?>
- </div>
- </div>
- <div class="layui-inline">
- <div class="layui-input-inline" style="width: 60px">
- <?= Html::activeTextInput($moduleModel, 'house_ting', [
- 'class' => 'layui-input',
- 'required' => '',
- 'lay-verify' => 'required|number',
- 'autocomplete' => 'off'
- ]) ?>
- </div>
- <label class="layui-form-label" style="width: 10px; padding:9px 0px;">厅</label>
- </div>
- <div class="layui-inline">
- <div class="layui-input-inline" style="width: 60px">
- <?= Html::activeTextInput($moduleModel, 'house_wei', [
- 'class' => 'layui-input',
- 'required' => '',
- 'lay-verify' => 'required|number',
- 'autocomplete' => 'off'
- ]) ?>
- </div>
- <label class="layui-form-label" style="width: 10px; padding:9px 0px;">卫</label>
- </div>
- <div class="layui-inline">
- <label class="layui-form-label" style="width: 80px; padding:9px 10px;">共</label>
- <div class="layui-input-inline" style="width: 60px">
- <?= Html::activeTextInput($moduleModel, 'house_area', [
- 'class' => 'layui-input',
- 'required' => '',
- 'lay-verify' => 'required|isFloat',
- 'autocomplete' => 'off'
- ]) ?>
- </div>
- <label class="layui-form-label" style="width: 60px; padding:9px 0px;">㎡面积</label>
- </div>
- </div>
- </div>
PHP Code复制内容到剪贴板
- <div class="layui-form-item required">
- <label class="layui-form-label control-label">房屋参数</label>
- <div class="layui-input-block">
- <div class="layui-input-inline">
- <?= Html::activeDropDownList($moduleModel, 'house_object_type', $moduleModel->getHouseObjectType(), [
- 'class' => 'layui-input'
- ]) ?>
- </div>
- <div class="layui-input-inline">
- <?= Html::activeDropDownList($moduleModel, 'house_fit_type', $moduleModel->getHouseFitType(), [
- 'class' => 'layui-input'
- ]) ?>
- </div>
- <div class="layui-input-inline">
- <?= Html::activeDropDownList($moduleModel, 'house_toward', $moduleModel->getHouseToward(), [
- 'class' => 'layui-input'
- ]) ?>
- </div>
- </div>
- </div>
- <?=
- $form->field($moduleModel, 'house_want_type')
- ->radioList(
- $moduleModel->getHouseWantType(),
- [
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : $index == 0 ? "checked" : "";
- $template = '<input type="radio" ' . $label . '" name="' . $name . '" value="' . $index . '" title="' . $label . '" ' . $c . '>';
- return $template;
- }
- ]
- )
- ?>
- <?=
- $form->field($moduleModel, 'house_pay_way')
- ->radioList(
- $moduleModel->getHousePayWay(),
- [
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : $index == 0 ? "checked" : "";
- $template = '<input type="radio" ' . $label . '" name="' . $name . '" value="' . $index . '" title="' . $label . '" ' . $c . '>';
- return $template;
- }
- ]
- )
- ?>
- <?=
- $form->field($moduleModel, 'house_type')
- ->radioList(
- $moduleModel->getHouseType(),
- [
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : $index == 0 ? "checked" : "";
- $template = '<input type="radio" ' . $label . '" name="' . $name . '" value="' . $index . '" title="' . $label . '" ' . $c . '>';
- return $template;
- }
- ]
- )
- ?>
- <?= $form->field($moduleModel, 'house_address')->textInput(['maxlength' => true, 'required' => '', 'lay-verify' => 'required', 'autocomplete' => 'off', 'placeholder' => '可记录几号楼几室、房主信息等(仅内部系统展示)']) ?>
添加一个默认值 和写一个form的表单就不再使用Html来拼接div了:
PHP Code复制内容到剪贴板
- <?php
- $model->phone = Yii::$app->user->identity->username;
- $model->house_price = $price;
- $model->house_address = $address;
- ?>
- <?php $form = ActiveForm::begin([
- 'enableAjaxValidation' => true,
- 'options' => ['class' => 'form-horizontal form-padding'],
- 'fieldConfig' => [
- 'template' => "{label}\n<div class=\"col-md-8\">{input}</div>\n<div class=\"col-md-8 col-md-offset-3\">{error}</div>",
- 'labelOptions' => ['class' => 'col-sm-3 control-label'],
- ],
- ]); ?>
- <?= Html::activeHiddenInput($model, 'apply_id', ['value' => Yii::$app->request->get("id", 0)]) ?>
- <?= Html::activeHiddenInput($model, 'user_id', ['value' => Yii::$app->user->id]) ?>
- <fieldset>
- <div class="form-group">
- <label for="house" class="col-sm-3 control-label">申请房源</label>
- <div class="col-md-8">
- <?= $area ? $area . " - " : "" ?><?= $district ? $district . " - " : "" ?><?= $title ?>
- </div>
- </div>
- <?= $form->field($model, 'truename') ?>
- <?= $form->field($model, 'phone') ?>
- <?= $form->field($model, 'house_address') ?>
- <?= $form->field($model, 'house_price_type')->dropDownList($model->getHousePriceType()) ?>
- <?= $form->field($model, 'house_price') ?>
- <?= $form->field($model, 'pay_datetime')->textInput(['data-toggle' => 'datepicker']) ?>
- <?= $form->field($model, 'employer') ?>
- <?= $form->field($model, 'mark')->textarea(['rows'=>5]) ?>
- <div class="form-group">
- <div class="col-md-8 col-md-offset-3">
- <?= Html::submitButton('立即提交', ['class' => 'btn btn-success btn-lg']) ?>
- </div>
- </div>
- </fieldset>
- <?php ActiveForm::end(); ?>
PHP Code复制内容到剪贴板
- <?php $form = ActiveForm::begin([
- 'action' => ['test/getpost'], //可以忽略
- 'method' => 'post',
- 'id' =>'staffForm',
- 'enableAjaxValidation' => true, //必须设置,表示该表单以ajax提交
- ]); ?>
- <!-- 两种隐藏文本框写法-->
- <?= Html::activeHiddenInput($model, 'id') ?>
- <?= Html::hiddenInput("aid", Yii::$app->request->get("aid")) ?>
- <?php ActiveForm::end(); ?>
- <!-- JS注册到底部 -->
- <?php $this->beginBlock('js') ?>
- <script>
- $(function () {
- $(document).on('beforeSubmit', 'form#staffForm', function () {
- var form = $(this);
- var loading = layer.load(2);
- //返回错误的表单信息
- if (form.find('.has-error').length) {
- return false;
- }
- //表单提交
- $.ajax({
- url: form.attr('action'),
- type: 'post',
- data: form.serialize(),
- success: function (res) {
- layer.close(loading);
- if(res.errcode){
- layer.open({
- title: '错误'
- ,content: res.errmsg
- });
- }else{
- layer.msg(res.errmsg, {
- time: 1000 // 2秒关闭(如果不配置,默认是3秒)
- }, function(){
- history.go(-1);
- });
- }
- },
- error: function () {
- layer.open({
- title: '错误'
- ,content: '系统错误,请联系管理员'
- });
- return false;
- }
- });
- return false;
- });
- });
- </script>
- <?php $this->endBlock() ?>
PHP Code复制内容到剪贴板
- <?php $form = ActiveForm::begin([
- 'id' => 'form-signup',
- 'options' => [
- 'class' => 'layui-form',
- ],
- 'fieldConfig' => [
- 'options' => [
- 'class' => 'layui-form-item',
- ],
- 'template' => '{label}<div class="layui-input-inline">{input}</div>{error}',
- 'labelOptions' => ['class' => 'layui-form-label control-label'],
- 'errorOptions' => [
- 'class' => 'layui-form-mid layui-word-aux', //替换掉错误提示的help-block
- ],
- 'inputOptions' => [
- 'class' => 'layui-input', //替换掉input表单的form-control
- ]
- ],
- ]); ?>
- <?= $form->field($model, 'username') ?>
- <?= $form->field($model, 'password')->passwordInput() ?>
- <?= $form->field($model, 'retypePassword')->passwordInput() ?>
- <?= $form->field($model, 'email') ?>
- <?= $form->field($model, 'group_id')->dropDownList(\common\modules\user\models\Group::getGroupList()) ?>
- <?= $form->field($model, 'user_fen') ?>
- <?= $form->field($model, 'money') ?>
- <?= $form->field($model, 'user_date') ?>
- <?= $form->field($model, 'z_group_id')->dropDownList(\common\modules\user\models\Group::getGroupList())->label("到期后转向") ?>
- <?= $form->field($model, 'checked')->dropDownList($model->getCheck())->label("状态") ?>
- <div class="layui-form-item">
- <label class="layui-form-label control-label"></label>
- <div class="layui-input-inline">
- <?= Html::submitButton("新增", ['class' => 'layui-btn', 'name' => 'signup-button']) ?>
- </div>
- </div>
- <?php ActiveForm::end(); ?>
css,js需要添加:
XML/HTML Code复制内容到剪贴板
- <style>
- .has-error .layui-form-mid.layui-word-aux {
- color: #a94442 !important;
- }
- .layui-form-label.control-label {
- width: 200px;
- }
- </style>
- <?php $this->beginBlock('js') ?>
- <script>
- layui.use(['form'], function () {
- var form = layui.form;
- });
- </script>
- <?php $this->endBlock() ?>
activeInput / activeDropDownList
PHP Code复制内容到剪贴板
- <?= Html::activeInput('text', $model, 'plan_time', ['class' => 'form-control', 'autocomplete' => 'off','value'=>date("Y-m-d") ]) ?>
PHP Code复制内容到剪贴板
- <?= Html::activeInput('text', $model, 'plan_time', ['class' => 'form-control', 'id' => 'layui-input-date0', 'value' => $model->plan_time ? date("Y-m-d", $model->plan_time) : date("Y-m-d")]) ?>
PHP Code复制内容到剪贴板
- <?= Html::activeHiddenInput($model, 'plan_time', ['value'=>date("Y-m-d")]) ?>
PHP Code复制内容到剪贴板
- Html::dropDownList('user_id',$user_id,\backend\modules\user\models\User::getDropDownListAllUser(), ['lay-search' => '']);
- Html::activeDropDownList($model, 'apply_user_id', \backend\modules\user\models\User::getDropDownListByProjectId(), ['prompt' => '不限', 'lay-search' => '']);
PHP Code复制内容到剪贴板
- <?= Html::activeTextarea($model, 'reason', ['class' => 'form-control']) ?>
PHP Code复制内容到剪贴板
- <?= Html::activeRadioList($model, 'pay_type', [0 => "合同", 1 => "非合同"], [
- // 'unselect' => null,
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : "";
- $template = '<input type="radio" ' . $label . ' name="' . $name . '" value="' . $value . '" title="' . $label . '" ' . $c . '>';
- return $template;
- }
- ]) ?>
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'project_id')->checkboxList(\backend\models\Project::getAllDropDownList(), [
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : "";
- $template = '<p class="mb10"><input type="checkbox" ' . $label . ' name="' . $name . '" value="' . $value . '" title="' . $label . '" ' . $c . '></p>';
- return $template;
- }
- ]) ?>
PHP Code复制内容到剪贴板
- <div class="form-group row field-enewsclass-islast required">
- <label class="col-sm-2 col-form-label text-right">是否终极栏目</label>
- <div class="col-sm-6">
- <?= Html::activeCheckboxList($model, "islast", ['1' => '是'], [
- 'class'=>'form-check',
- 'tag'=>false,
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : "";
- $template = '<input class="form-check-input" type="checkbox" name="' . $name . '" value="' . $index . '" title="' . $label . '" ' . $c . '><label class="form-check-label"> ' . $label . '</label>';
- return $template;
- },
- ]); ?>
- </div>
- </div>
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'islast')->checkboxList(
- ['1' => '是'],
- [
- 'item' => function ($index, $label, $name, $checked, $value) {
- $c = $checked ? "checked" : "";
- $template = '<input class="form-check-input" type="checkbox" name="' . $name . '" value="' . $index . '" title="' . $label . '" ' . $c . '><label class="form-check-label"> ' . $label . '</label>';
- return $template;
- },
- 'class' => 'form-check'
- ]
- ) ?>