控制器:(写了貌似也没用,未解决验证码位数;位数可改核心代码)
public $layout = false;//隐藏导航
public function actions(){
return [
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>[
'class' => 'yiicaptchaCaptchaAction',
'backColor'=>0xFFFFFF, //背景颜色
'minLength'=>4, //最短为4位
'maxLength'=>4, //是长为4位
'transparent'=>true, //显示为透明
'testLimit'=>0,
'fixedVerifyCode' => YII_ENV_TEST ? 'test' : null,
],
];
}
模型层:
//需要定义verifyCode
Public $verifyCode
public function rules()
{
return [//验证码
['verifyCode', 'required', 'message' => '验证码不可以为空'],
['verifyCode', 'captcha'],
];
}
视图层:
use yiicaptchaCaptcha;(需要应用这个小组件)
//使用表单组件和验证组件
use yiiwidgetsActiveForm;
use yiicaptchaCaptcha;
//生成表单
$form = ActiveForm::begin([
'id' => 'contact-form',
"action"=>["vip/az"]
]);
//显示验证码
echo $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>',
]) ;
echo "<input type='submit' value='提交'/>";
ActiveForm::end();