1111、model
<?php namespace frontend\models; use yii; use yii\base\model; class Tu extends Model{ public $verifyCode; public $username; public $password; public function rules() { return [ ['username','required'], ['password','required'], ['verifyCode', 'required'], ['verifyCode', 'captcha'], ]; } public function atrributeLabels(){ return array( 'verifyCode'=>'验证码', ); } } 2222、controller<?php namespace frontend\controllers; use yii; use yii\web\Controller; use frontend\models\Tu; class TuController extends Controller{ public function actionIndex(){ $model = new Tu(); return $this->render('login', ['model' => $model]); } }3333.view<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\captcha\Captcha; $form = ActiveForm::begin([ 'id' => 'login-form', 'options' => ['class' => 'form-horizontal'], ]) ?> <?= $form->field($model, 'username') ?> <?= $form->field($model, 'password')->passwordInput() ?> <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [ 'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>', ]) ?> <div class="form-group"> <div class="col-lg-offset-1 col-lg-11"> <?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?> </div> </div> <?php ActiveForm::end() ?>