【问题标题】:Yii captcha validation check does not workYii 验证码验证检查不起作用
【发布时间】:2015-02-04 15:32:58
【问题描述】:

这是我的模型:

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', 'on' => 'insert'),
                // 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!"),
            );
        }


        return array(
            array('id, lastname, email, phone, dob, requisition_id, experienceMonths, experienceYears, sync, cv_path, created', 'safe', 'on' => 'search'),
            array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'captchaRequired'),
        );
    }

我的问题是它不验证图像中的字母。我不知道为什么。我认为我的验证在规则中是不正确的。任何人有任何线索?

【问题讨论】:

    标签: yii captcha


    【解决方案1】:

    是的,您有很多场景并对其应用不同的规则。

    确保您当前运行的方案确实有验证规则。

    我可以看到您以两种方式使用场景,一种是您使用的条件,我不完全确定它们是否有效,但也许可以。

    第二种是 Yii 方式,通过在每个规则声明中的 'on' 属性中指定。

    我建议在这个结构中从头开始重新编写所有规则:

    return array(
        // scenarioA
        array('field1, field2', 'required', 'on' => 'scenarioA'),
        array('field1, field2', 'required', 'on' => 'scenarioA'),
        array('field1, field2', 'required', 'on' => 'scenarioA'),
        // scenarioB
        array('field3, field4', 'required', 'on' => 'scenarioB'),
        array('field3, field4', 'required', 'on' => 'scenarioB'),
        array('field3, field4', 'required', 'on' => 'scenarioB'),
        // scenarioC
        array('field5, field6', 'required', 'on' => 'scenarioC'),
        array('field5, field6', 'required', 'on' => 'scenarioC'),
        array('field5, field6', 'required', 'on' => 'scenarioC'),
    );
    

    或者,如果您测试它们的行为是否正确,您可以保留您的条件块解决方案,但在这种情况下,您应该从块内的规则中删除“on”参数。

    因为例如这里:

    if ($this->scenario == "insert") {
                return array(
                    ...
                    array('taleo_id, sync', 'required', 'on' => 'taleoUpdate'),
    

    您将代码放入验证场景为“插入”的条件中,但随后您再次指定“开启”,这将使该规则仅应用于“taleoUpdate”场景,因此没有任何意义。

    哦,至于验证码,正如您在格式中看到的那样,只有当场景与您在上面指定的场景不同时,您才能达到它的规则。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 2014-09-24
      • 2014-01-08
      • 1970-01-01
      相关资源
      最近更新 更多