Model层
<?php
namespace app\models\test;
use yii\db\ActiveRecord;
class Dynasty extends ActiveRecord //或者 \yii\base\Model
{
public $username;
public $password;
}
controller层
use yii\web\Controller;
use app\models\test\Dynasty;
class TestController extends Controller
{
public function actionIndex()
{
return $this->render('index', [
'model' => new Dynasty(),
]);
}
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['id' => 'login-form','options' => ['class' => 'class_name'],'action'=>'test/index','method'=>'get',]); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Login') ?>
</div>
<?php ActiveForm::end(); ?>
表单类型,redio,input等
密码
<?= $form->field($model, 'password')->passwordInput() ?>
标签与提示
<?= $form->field($model, 'username')->textInput()->hint('请输入你的用户名')->label('用户名') ?>
<?= $form->field($model, 'username[]',['inputOptions'=>['value'=>'abc','class'=>'form-control']]) ?>//默认值
邮箱
<?= $form->field($model, 'username')->input('email') ?>
上传
<?= $form->field($model, 'username')->fileInput(['multiple'=>'multiple']) ?>
多选列表
<?= $form->field($model, 'username[]')->checkboxList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>
单个选择框
<?= $form->field($model, 'username')->checkbox([],false)->label('已审核') ?> ?>
下拉列表
<?= $form->field($model, 'username[]')->dropDownList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>
<?= $form->field($model, 'username[]')->dropDownList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c'], ['prompt' => '多选b']) ?>
隐藏框
<?= $form->field($model, 'username')->hiddenInput(['1']) ?>
ListBox
<?= $form->field($model, 'username[]')->listBox(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>
单选列表
<?= $form->field($model, 'username[]')->radioList(['a' => '单选a', 'b' => '单选b', 'c' => '单选c']) ?>
多行文本
<?= $form->field($model, 'username')->textarea() ?>
widget扩展
<?= $form->field($model, 'username')->widget(\yii\widgets\MaskedInput::className(), ['mask' => '9999/99/99',]); ?>