熊本污 2016-05-03 18:51:14 3277次浏览 0条评论 0 0 0

1 在任意controller里面重写方法(在控制器层添加以下代码在相应的方法内)

public function actions()
{
    return [
         'captcha' => [
              'class' => 'yii\captcha\CaptchaAction',
              'maxLength' => 5,
              'minLength' => 5
         ],
     ];
 }

2 表单模型(将下列数据添加到模型的相应地方)

    public $verifyCode;
     
    public function rules()
    {
        return [
            ['verifyCode', 'required'],
            ['verifyCode', 'captcha'],
        ];
    }

3 视图(在视图层添加以下代码在相应位置)

     use yii\captcha\Captcha;

    <?= $form->field($model, 'verifyCode', [
        'options' => ['class' => 'form-group form-group-lg'],
    ])->widget(Captcha::className(),[
       'template' => "{image}{input}",
       'imageOptions' => ['alt' => '验证码'],
       'captchaAction' => 'site/captcha',
    ]); ?>
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册