Yii2 ActiveForm 用法
模板
<?php $form = ActiveForm::begin() ?>
<?= $form->field($model, 'id')->hiddenInput() ?>
<?= $form->field($model, 'title')->textInput() ?>
<?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?= Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?php ActiveForm::end() ?>
表单样式
<?php
$form = ActiveForm::begin([
'id' => 'msg-form',
'options' => ['class'=>'form-horizontal'],
'fieldConfig' => [
'template' => "{label}{input}\n{error}",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
]
]);
注:{label}:label标签 {input}:表单域,可能是文本域、下拉框 {error}:错误提示信息
表单
//文本域
<?= $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>
//密码框
<?= $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?>
//单选按钮
<?= $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?>
//下拉选择框
<?= $form->field($model, 'edu')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px']) ?>
//文件上传
<?= $form->field($model, 'file')->fileInput() ?>
//复选框
<?= $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>
//多行文本域
<?= $form->field($model, 'info')->textarea(['rows'=>3]) ?>
//隐藏域
<?= $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?>
//提交
<?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
//重置
<?= Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
小部件
//日期小部件
<?= kartik\date\DatePicker::widget(['name'=>'date']) ?>
//时间日期小部件
<?= $form->field($model, 'created_at')->widget(kartik\datetime\DateTimePicker::classname(), [
'options' => ['placeholder' => ''],
'pluginOptions' => [
'autoclose' => true,
'todayHighlight' => true,
]
]); ?>
标签:btn,name,button,label,ActiveForm,用法,field,model,Yii2
From: https://www.cnblogs.com/hu308830232/p/16825121.html