Example #1
0
 /**
  * Client validation
  *
  * @param Model $model
  * @param string $attribute
  * @param View $view
  * @return string javascript method
  */
 public function clientValidateAttribute($model, $attribute, $view)
 {
     $label = $model->getAttributeLabel($attribute);
     $options = ['strError' => Html::encode(Yii::t('pwdstrength', $this->message, ['attribute' => $label]))];
     $options['userField'] = '#' . Html::getInputId($model, $this->userAttribute);
     foreach (self::$_rules as $rule => $setup) {
         $param = "{$rule}Error";
         if ($this->{$rule} !== null) {
             $options[$rule] = $this->{$rule};
             $options[$param] = Html::encode(Yii::t('pwdstrength', $this->{$param}, ['attribute' => $label]));
         }
     }
     StrengthValidatorAsset::register($view);
     return "kvStrengthValidator.validate(value, messages, " . Json::encode($options) . ");";
 }
Example #2
0
 /**
  * Adds an error about the specified attribute to the active record.
  * This is a helper method that performs message selection and internationalization.
  *
  * @param \Model $object    the data object being validated
  * @param string $attribute the attribute being validated
  * @param string $message   the error message
  * @param array  $params    values for the placeholders in the error message
  */
 protected function addError($object, $attribute, $message, $params = [])
 {
     $params['{attribute}'] = $object->getAttributeLabel($attribute);
     $object->addError($attribute, strtr($message, $params));
 }
 /**
  * Validates the attribute of the object.
  * If there is any error, the error message is added to the object.
  *
  * @param \Model $object    the object being validated
  * @param string $attribute the attribute being validated
  *
  * @throws \Kohana_Exception if invalid operator is used
  */
 protected function validateAttribute($object, $attribute)
 {
     $value = $object->{$attribute};
     if ($this->allowEmpty && $this->isEmpty($value)) {
         return;
     }
     if ($this->compareValue !== null) {
         $compareTo = $compareValue = $this->compareValue;
     } else {
         $compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
         $compareValue = $object->{$compareAttribute};
         $compareTo = $object->getAttributeLabel($compareAttribute);
     }
     switch ($this->operator) {
         case '=':
         case '==':
             if ($this->strict && $value !== $compareValue || !$this->strict && $value != $compareValue) {
                 $message = $this->message !== null ? $this->message : '{attribute} must be repeated exactly.';
             }
             break;
         case '!=':
             if ($this->strict && $value === $compareValue || !$this->strict && $value == $compareValue) {
                 $message = $this->message !== null ? $this->message : '{attribute} must not be equal to "{compareValue}".';
             }
             break;
         case '>':
             if ($value <= $compareValue) {
                 $message = $this->message !== null ? $this->message : '{attribute} must be greater than "{compareValue}".';
             }
             break;
         case '>=':
             if ($value < $compareValue) {
                 $message = $this->message !== null ? $this->message : '{attribute} must be greater than or equal to "{compareValue}".';
             }
             break;
         case '<':
             if ($value >= $compareValue) {
                 $message = $this->message !== null ? $this->message : '{attribute} must be less than "{compareValue}".';
             }
             break;
         case '<=':
             if ($value > $compareValue) {
                 $message = $this->message !== null ? $this->message : '{attribute} must be less than or equal to "{compareValue}".';
             }
             break;
         default:
             throw new \Kohana_Exception('Invalid operator "{operator}".', ['{operator}' => $this->operator]);
     }
     if (!empty($message)) {
         $this->addError($object, $attribute, $message, ['{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue]);
     }
 }