加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – yii captcha无法正确验证

发布时间:2020-05-25 08:58:46 所属栏目:PHP 来源:互联网
导读:我试图使用yii添加验证码到我的联系表单,但验证有一些问题. 我的模特 class ContactForm extends CFormModel{ public $verifyCode; public function rules(){ return array( array(verifyCode, captcha, allowEmpty=!CC

我试图使用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视图中的代码

<form action="" method="post">
            <?php
               $form=$this->beginWidget('CActiveForm',array(
                    'id'=>'contact-form','enableAjaxValidation'=>false,));
            ?>
           <?php if(CCaptcha::checkRequirements()){ ?>  
               <div class="row">
         <div class="contact_field_wrapper">
              <?php echo '<b>ARE YOU HUMAN?</b><br />'.$form->labelEx($model,'verifyCode'); ?> 
<div class="captcha user-captcha">
         <?php $this->widget('CCaptcha',array(  'captchaAction'=>'site/captcha' ));                                                                                         
  ?>
                                <?php echo $form->error($model,'verifyCode'); ?>
                                <?php  echo '<br />'.$form->textField($model,'verifyCode'); ?>
                                <div class="hint">Please enter the letters as they are shown in the image above.<br/>
                                    Letters are not case-sensitive.
                                </div>
                            </div>
                            </div>
                            </div>
<?php } ?>
 <?php $this->endWidget(); ?>
</form>

问题是$model-> validate()在输入正确的代码时返回false.
$model-> getErrors()始终返回’验证码不正确’.

你的模型是空的,因为你没有将任何值传递给$model-> attriburtes

做这个 :

if($_POST['ContactForm'])
{
     $model->attributes() = $_POST['ContactForm'];
     if($model->validate())
     {
           //some code
     }
}
$this->render('advertise',array('model' => $model));

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读