Exemplo n.º 1
0
 /**
  * Validates the attribute of the object.
  * If there is any error, the error message is added to the object.
  * @param CModel $object the object being validated
  * @param string $attribute the attribute being validated
  */
 protected function validateAttribute($object, $attribute)
 {
     if (!$this->ajaxValidation && Yii::app()->request->isAjaxRequest) {
         return;
     }
     parent::validateAttribute($object, $attribute);
 }
 /**
  * Client (ajax) validator for extended captcha.
  * @param CModel $object the data object being validated
  * @param string $attribute the name of the attribute to be validated.
  * @return string the client-side validation script.
  * @see CActiveForm::enableClientValidation
  */
 public function clientValidateAttribute($object, $attribute)
 {
     $captcha = $this->getCaptchaAction();
     if ($captcha->mode == CaptchaExtendedAction::MODE_DEFAULT) {
         // default framework implementation
         return parent::clientValidateAttribute($object, $attribute);
     }
     $message = $this->message !== null ? $this->message : Yii::t('main', 'The verification code is incorrect.');
     $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute)));
     $result = $captcha->getVerifyResult();
     // remove whitespaces
     $result = preg_replace('/\\s/', '', $result);
     if (!$this->caseSensitive) {
         $result = mb_convert_case($result, MB_CASE_LOWER, 'utf-8');
     }
     $result = urlencode($result);
     $hash = $captcha->generateValidationHash($result);
     $js = "\nvar hash = \$('body').data('{$this->captchaAction}.hash');\nif(hash == null){\n\thash = {$hash};\n}else{\n\thash = hash[" . ($this->caseSensitive ? 0 : 1) . "];\n}\nvalue = value.replace(/\\s/g,'');\n" . ($this->caseSensitive ? '' : 'value = value.toLowerCase();') . "\nvalue = encodeURIComponent(value);\nfor(var i=value.length-1, h=0; i >= 0; --i){\n\th+=value.charCodeAt(i);\n}\nif(h != hash) {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n";
     if ($this->allowEmpty) {
         $js = "\nif(value!=''){\n\t{$js}\n}\n";
     }
     return $js;
 }
Exemplo n.º 3
0
 /**
  * Checks config
  * This is the 'checkConfig' validator as declared in rules().
  */
 public function checkConfig($attribute, $params)
 {
     //if owner_name class exists in configuration
     if (count($this->config) === 0) {
         if ($attribute === 'owner_name') {
             $this->addError($attribute, Yii::t('CommentsModule.msg', 'This item cann\'t be commentable'));
         }
         return;
     }
     //if only registered users can post comments
     if ($attribute === 'creator_id' && ($this->config['registeredOnly'] === true || Yii::app()->user->isGuest === false)) {
         unset($this->user_email, $this->user_name);
         $numberValidator = new CNumberValidator();
         $numberValidator->allowEmpty = false;
         $numberValidator->integerOnly = true;
         $numberValidator->attributes = array('creator_id');
         $numberValidator->validate($this);
     }
     //if se captcha validation on posting
     if ($attribute === 'verifyCode' && $this->config['useCaptcha'] === true) {
         $captchaValidator = new CCaptchaValidator();
         $captchaValidator->caseSensitive = false;
         $captchaValidator->captchaAction = Yii::app()->urlManager->createUrl(CommentsModule::CAPTCHA_ACTION_ROUTE);
         $captchaValidator->allowEmpty = !CCaptcha::checkRequirements();
         $captchaValidator->attributes = array('verifyCode');
         $captchaValidator->validate($this);
     }
     //if not only registered users can post comments and current user is guest
     /*if (($attribute === 'user_name' // || $attribute === 'user_email'
                 ) && ($this->config['registeredOnly'] === false && Yii::app()->user->isGuest === true))
             {
                 unset($this->creator_id);
                 $requiredValidator = new CRequiredValidator();
                 $requiredValidator->attributes = array($attribute);
                 $requiredValidator->validate($this);
                 $stringValidator = new CStringValidator();
                 $stringValidator->max = 128;
                 $stringValidator->attributes = array($attribute);
                 $stringValidator->validate($this);
                 //if($attribute === 'user_email')
     //            {
     //                $emailValidator = new CEmailValidator();
     //                $emailValidator->attributes = array('user_email');
     //                $emailValidator->validate($this);
     //            }
             }*/
 }