标签:mode next widgets mod 部件 apt pos orm his
首先知晓我们在使用验证码的时候通常是和我们的表单小部件配合使用
首先我们创建model层
新建一个php文件 名字叫做Verifycode.php
要在我们的model层 创建我们的验证码的验证规则,我这里简单的写两个规则 一个是 非空 另一个是验证码必须验证正确
规则写在我们的model的rules里
<?php
/**
* Created by PhpStorm.
* User: jinlei
* Date: 2017/2/13
* Time: 13:57
*/
namespace frontend\models;
use yii\base\Model;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
class Verifycode extends Model
{
/*定义需要使用的标量*/
public $verifycode;
public function rules(){
return[
[‘verifycode’,’required’],
[‘verifycode’, ‘captcha’],
];
}
public function attributeLabels()
{
return array(
‘verifycode’=>’验证码’,
);
//’rememberMe’=>’Remember me next time’,
}
}
然后新建控制器层 创建一个php文件名字叫做VerifycodeController.php
<?php
/**
* Created by PhpStorm.
* User: jinlei
* Date: 2017/2/13
* Time: 14:03
*/
namespace frontend\controllers;
use yii\web\Controller;
use frontend\models\Verifycode;
class VerifycodeController extends Controller
{
public function actionIndex(){
$model = new Verifycode();
return $this->render(‘index’,[‘model’=>$model]);
}
}
接下来创建view层
新建一个php文件 名字叫做index.php
<?php
/**
* Created by PhpStorm.
* User: jinlei
* Date: 2017/2/13
* Time: 14:07
*/
use yii\captcha\Captcha;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
$form = ActiveForm::begin([
‘id’ => ‘login-form’,
‘options’ => [‘class’ => ‘form-horizontal’],
‘action’=>’?r=index/login’,
‘method’=>’post’,
]) ?>
<?= $form->field($model, ‘verifycode’)->widget(Captcha::className()) ?>
<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() ?>
标签:mode next widgets mod 部件 apt pos orm his
原文地址:https://www.cnblogs.com/cjymuyang/p/9448615.html