【问题标题】:yii captcha not validating properlyyii 验证码未正确验证
【发布时间】:2014-01-08 13:58:38
【问题描述】:

我正在尝试使用 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));
    }
 }
}

我的 Advertisement.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-&gt;validate() 在输入正确代码时返回 false。 $model-&gt;getErrors() 总是返回'验证码不正确'。

【问题讨论】:

  • 有没有办法以某种方式调试这个问题?
  • 您的模型为空,验证返回 false
  • 'contact' 是表格的名称吗?我没有在数据库中存储任何值
  • 非常感谢!那成功了。我添加了 if(isset($_POST['ContactForm']){ $model->attributes=$_POST['ContactForm'];} 你能在答案中提及它以便我接受吗?

标签: php validation yii captcha


【解决方案1】:

你的模型是空的,因为你没有向$model-&gt;attriburtes传递任何值

这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    相关资源
    最近更新 更多