yii captcha无法正确验证

 淘宝杂谈网z 发布于 2023-02-09 09:55

我试图使用yii添加验证码到我的联系表单,但验证有一些问题.

我的模特

class ContactForm extends CFormModel
{
  public $verifyCode;

 public function rules()
{
    return array(

        array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'captchaRequired'),
        array('verifyCode', 'safe'),
                );
}
}

我控制器中的代码

public function filters()
{
    return array(
        'accessControl',
    );
}



public function accessRules()
{
    return array(
            array(  'allow', //allow all users to perform advertise and index action 
                    'actions' => array('advertise','index', 'captcha'),
        'users' => array('*'),
            ),
    );
}

 public function actions() {
    return array(
        // captcha action renders the CAPTCHA image displayed on the contact page
        'captcha' => array(
            'class' => 'CCaptchaAction',
            'backColor' => 0xFFFFFF,
            'testLimit'=> '2',
        ),
      )
}

 public actionAdvertise()
 {   $model = new ContactForm;
    $model->scenario = 'captchaRequired';
    if($model->validate()){
   //some code
    }  else {
            $this->render('advertise', array('model' => $model));
    }
 }
}

我的advertise.php视图中的代码

 
beginWidget('CActiveForm',array( 'id'=>'contact-form', 'enableAjaxValidation'=>false, )); ?>
ARE YOU HUMAN?
'.$form->labelEx($model, 'verifyCode'); ?>
widget('CCaptcha',array( 'captchaAction'=>'site/captcha' )); ?> error($model, 'verifyCode'); ?> '.$form->textField($model,'verifyCode'); ?>
Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.
endWidget(); ?>

问题是$model->validate()当输入正确的代码时返回false. $model->getErrors()总是返回'验证码不正确'.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有