iceluo 2015-09-06 17:56:19 8103次浏览 5条评论 9 5 0

yii2 验证码详细代码,有错误请指出
控制器 层 LoginController.php

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Login;
use app\models\ContactForm;
class LoginController extends Controller
{
    public $enableCsrfValidation = false;
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
    public function actionLogin()
    {
        // 验证码
        $verify = new Login();

        return $this->render('login', [
            'verify' => $verify,
        ]);
    }
}

models 层 Login.php

<?php
namespace app\models;
use Yii;
use yii\base\Model;
class Login extends Model
{
    public $verifyCode;
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // verifyCode needs to be entered correctly
            ['verifyCode', 'captcha'],
        ];
    }
    /**
     * @return array customized attribute labels
     */
    public function attributeLabels()
    {
        return [
            'verifyCode' => '验证码',
        ];
    }
}

view 层 login.php

<?php
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
?>
<div class="row">
    <div class="col-lg-5">
        <form action="">
        <?php $form = ActiveForm::begin(); ?>
        <?= $form->field($verify, 'verifyCode')->widget(Captcha::className(), [
            'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
        ]) ?>
        <?php ActiveForm::end(); ?>
            // 这里面可以加你想要的任何表单
            <input type="submit" class="btn" value="提交"/>
        </form>
    </div>
</div>
觉得很赞
  • 评论于 2015-09-16 16:34 举报

    嗯 收藏了

  • 评论于 2015-10-08 19:06 举报

    view 层 login.php template {label} 和 {error} 如何自定义?这两个没有解析直接显示,还请教如何自定义?

    6 条回复
    评论于 2015-10-09 10:14 回复

    可以再models类里面定义
    你看看下面代码,希望可以帮助到你

    public function rules()

    {
        return [
            // name, email, subject and body are required
            [['username'], 'required','message'=>'用户名不能为空'],
            [[ 'password'], 'required','message'=>'密码不能为空'],
            // verifyCode needs to be entered correctly
            ['verifyCode', 'required','message'=>'验证码不能为空'],
            ['verifyCode', 'captcha','message'=>'验证码不正确'],
        ];
    }
    
    评论于 2015-10-12 10:08 回复

    加了,还是没有提示信息啊

    评论于 2015-10-12 10:28 回复

    你的包是2.0.6吗,如果是的话,有自带验证码的,你可以看看,好像是contact这个页面

    评论于 2015-10-12 10:59 回复

    恩,好的

    评论于 2015-10-15 01:40 回复

    field 里面单独定义 template 解决了

    评论于 2015-10-29 16:11 回复

    显示无效的参数,说viewsfile不存在,不知道怎么回事

    觉得很赞
  • 评论于 2016-05-11 20:06 举报

    Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message 'Invalid CAPTCHA action ID: site/captcha'

    in C:\wamp\www\TrafficViolation\vendor\yiisoft\yii2\captcha\CaptchaValidator.php:81
    这是什么问题呢?

  • 评论于 2016-09-28 13:38 举报

    前台验证成功,表单提交就提示验证码错误;原因可能是ajax验证一次 致使验证码在session中已经变了 再提交就提示错误了 怎么解决?

  • 评论于 2017-09-25 19:52 举报

    为什么图片不动啊

您需要登录后才可以评论。登录 | 立即注册