CaptchaValidator should be used together with CaptchaAction. Note that once CAPTCHA validation succeeds, a new CAPTCHA will be generated automatically. As a result, CAPTCHA validation should not be used in AJAX validation mode because it may fail the validation even if a user enters the same code as shown in the CAPTCHA image which is actually different from the latest CAPTCHA code.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\validators\Validator
 /**
  * @inheritdoc
  * 
  * Checks the CaptchaControl object in order to know if validation is needed 
  * or not.
  * Performs a hit in the captcha control cache component.
  */
 public function validateAttribute($model, $attribute)
 {
     $captchaControl = new CaptchaControl(['model' => $model]);
     if ($captchaControl->hasReachedRequestNumber()) {
         parent::validateAttribute($model, $attribute);
     }
     $captchaControl->hit();
 }
示例#2
0
 public function captcha()
 {
     $captcha = $this->captcha;
     $captchValidator = new CaptchaValidator();
     $captchValidator->captchaAction = '/admin/public/captcha';
     if (!$captchValidator->validate($captcha)) {
         $this->addError('captcha', '验证码错误!');
     }
 }
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['created_by', 'updated_by', 'created_at', 'updated_at', 'element_id', 'content_id', 'status'], 'integer'], [['element_id', 'rating'], 'required'], [['dignity', 'disadvantages', 'comments'], 'string'], [['rating'], 'integer', 'min' => 1, 'max' => (int) \Yii::$app->reviews2->maxValue], [['ip'], 'string', 'max' => 32], [['page_url'], 'string'], [['site_code'], 'string', 'max' => 15], [['user_name', 'user_email', 'user_phone', 'user_city'], 'string', 'max' => 255], [['status'], 'in', 'range' => array_keys(self::getStatuses())], ['site_code', 'default', 'value' => \Yii::$app->cms->site->code], ['published_at', 'integer'], ['processed_at', 'integer'], ['processed_by', 'integer'], ['user_email', 'email'], ['data_request', 'default', 'value' => $_REQUEST], ['data_server', 'default', 'value' => $_SERVER], ['data_cookie', 'default', 'value' => $_COOKIE], ['data_session', 'default', 'value' => function (self $model, $attribute) {
         if (\Yii::$app instanceof Application) {
             \Yii::$app->session->open();
             return $_SESSION;
         }
         return [];
     }], ['content_id', 'default', 'value' => function (self $model, $attribute) {
         return $model->element->cmsContent->id;
     }], ['ip', 'default', 'value' => function ($model) {
         if (\Yii::$app instanceof Application) {
             return \Yii::$app->request->userIP;
         }
         return null;
     }], ['verifyCode', \yii\captcha\CaptchaValidator::className(), 'captchaAction' => 'reviews2/backend/captcha', 'skipOnEmpty' => $this->_skipOnEmptyVerifyCode(), 'on' => self::SCENARIO_SITE_INSERT]];
 }