【问题标题】:Yii captcha weird behaviour of the message errorsYii captcha 消息错误的奇怪行为
【发布时间】:2015-02-05 17:19:29
【问题描述】:

您好,我正在努力实现 Yii 验证码,但我被卡住了。有一些奇怪的东西。

1. 即使我写对了,也会出现消息错误。我必须按“获取新代码”,然后它才能工作。看图片:

2. 我猜出于某种原因,此消息错误是由 on key 事件生成的。我只想在按下提交按钮时生成消息错误,该按钮执行 ajax 请求。

这是我的模型

public $verifyCode;
....
 public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.

        if ($this->scenario == "insert") {
            return array(
                array('requisition_id, sync', 'numerical', 'integerOnly' => true),
                array('lastname, firstname, email, dob, phone, cv_path, experienceMonths, experienceYears, competencies, token', 'required', 'message' => "Câmpul este obligatoriu"),
                array('email', 'email', 'message' => "Emailul este invalid!"),
                array('dob', 'validateDob'),
                array('dayOfBirth, monthOfBirth, yearOfBirth', 'safe'),
                array('taleo_id, sync', 'required', 'on' => 'taleoUpdate'),
                array('verifyCode', 'captcha', 'captchaAction'=>'site/captcha') 
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
            );
        } else if ($this->scenario == 'taleoUpdate') {
            return array(
                array('taleo_id, sync', 'required'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
            );
        }
        else if($this->scenario == 'notjobapply'){
            return array(
                array('lastname, firstname, email, phone, cv_path, requisition_id', 'required', 'message'=>'Câmpul este obligatoriu'),
                array('email', 'email', 'message' => "Emailul este invalid!"),
            );
        }
}

这是控制器

public function accessRules()
    {
        return array(
            array('allow', 
                'actions'=>array('aplica', 'captcha'),
                'users'=>array('*'),
            ),
        );
    } 
....
$this->render('apply', array(
            "applicant" => $model, 
            'job' => $job, 
            'region' => $region,
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFFFFFF,
            ),
        ));

最后是视图

<?php 

        if(CCaptcha::checkRequirements()){ ?>

        <?php echo $form->labelEx($applicant,'verifyCode'); ?><br>
        <?php  $this->widget('CCaptcha', array('captchaAction'=>'site/captcha')); ?> <br><br>
        <?php echo $form->textField($applicant,'verifyCode'); ?><br>

        <?php echo $form->error($applicant,'verifyCode'); ?><br><br>
    <?php } ?>

【问题讨论】:

    标签: php yii captcha


    【解决方案1】:

    对于用户模型验证规则,我使用这些:

    public function rules()
    {        
        return array(
            array('isActive, parentId, role, ban, isLegalEntity, organization, discount, paymentDelay, debtLimit, PaymentMethod, ShippingMethod, KnowingSource, scopeOfActivity, ShablonId, agree, Group', 'numerical', 'integerOnly'=>true), 
            array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on' => 'register'),    
            ...
         ),
     },     
    

    如果你尝试同样的方法会怎样:array('verifyCode', 'captcha', 'allowEmpty'=&gt;!CCaptcha::checkRequirements(),'on' =&gt; 'register')

    还要确保在你的控制器中你已经定义了这样的验证码操作:

    class UserController extends Controller
    {
       public function actions()
       {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
             'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFF2F2F,
             ),
          );
       }     
       ....
    }
    

    【讨论】:

    • 如果我让 'on' => 'register' 写什么都没关系,因为它通过了,结果无法正常工作。经过长时间的搜索,我找到了 smth,此代码: error($applicant,'verifyCode', array(), false, false); ?> 我在视图中添加了它正在工作。不要问我为什么。我不知道
    • 其实,第一次写对了就不行了,之后就自己刷新了,然后就正常了
    猜你喜欢
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多