php - yii 验证码没有正确验证

标签 php validation yii captcha

我正在尝试使用 yii 向我的联系表单添加验证码,但验证存在一些问题。

我的模型

class ContactForm extends CFormModel
{
  public $verifyCode;

 public function rules()
{
    return array(

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

Controller 中的代码

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 View 中的代码

 <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));

关于php - yii 验证码没有正确验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20691198/

相关文章:

php - (Yii)CDbCommand执行SQL语句失败: SQLSTATE[42000]: Syntax error or access violation:

php - MySQL 错误 - 无效的日期时间格式 : 1292 Incorrect datetime value: '' for column

javascript - 使用 Knockout 和复选框进行 ajax 调用时出现问题

带有 og 元标记的 php 重定向 url(打开图)

testing - 与 Yii 的持续集成

php - 从 PHP 启动独立的 Python 进程

php - 登录和注册页面 php 的问题

c - 如何验证结构体成员?

javascript - 使用正则表达式验证经纬度坐标然后显示错误

javascript - jQuery 验证插件 - 删除元素