diff --git a/framework/validators/BooleanValidator.php b/framework/validators/BooleanValidator.php index 46b8663..087e746 100644 --- a/framework/validators/BooleanValidator.php +++ b/framework/validators/BooleanValidator.php @@ -54,7 +54,7 @@ class BooleanValidator extends Validator } if (!$this->strict && $value != $this->trueValue && $value != $this->falseValue || $this->strict && $value !== $this->trueValue && $value !== $this->falseValue) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be either {true} or {false}.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be either {true} or {false}.'); $this->addError($object, $attribute, $message, array( '{true}' => $this->trueValue, '{false}' => $this->falseValue, @@ -70,7 +70,7 @@ class BooleanValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be either {true} or {false}.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be either {true} or {false}.'); $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, diff --git a/framework/validators/CaptchaValidator.php b/framework/validators/CaptchaValidator.php index 2bd8e9a..9fdf3fb 100644 --- a/framework/validators/CaptchaValidator.php +++ b/framework/validators/CaptchaValidator.php @@ -49,7 +49,7 @@ class CaptchaValidator extends Validator } $captcha = $this->getCaptchaAction(); if (!$captcha->validate($value, $this->caseSensitive)) { - $message = $this->message !== null ? $this->message : Yii::t('yii', 'The verification code is incorrect.'); + $message = $this->message !== null ? $this->message : \Yii::t('yii', 'The verification code is incorrect.'); $this->addError($object, $attribute, $message); } } @@ -85,7 +85,7 @@ class CaptchaValidator extends Validator public function clientValidateAttribute($object, $attribute) { $captcha = $this->getCaptchaAction(); - $message = $this->message !== null ? $this->message : Yii::t('yii', 'The verification code is incorrect.'); + $message = $this->message !== null ? $this->message : \Yii::t('yii', 'The verification code is incorrect.'); $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index 97febb5..08f230c 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -68,6 +68,7 @@ class CompareValidator extends Validator * If there is any error, the error message is added to the object. * @param \yii\base\Model $object the object being validated * @param string $attribute the attribute being validated + * @throws \yii\base\Exception if CompareValidator::operator is invalid */ public function validateAttribute($object, $attribute) { @@ -78,7 +79,7 @@ class CompareValidator extends Validator if ($this->compareValue !== null) { $compareTo = $compareValue = $this->compareValue; } else { - $compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; + $compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute; $compareValue = $object->$compareAttribute; $compareTo = $object->getAttributeLabel($compareAttribute); } @@ -87,37 +88,37 @@ class CompareValidator extends Validator case '=': case '==': if (($this->strict && $value !== $compareValue) || (!$this->strict && $value != $compareValue)) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be repeated exactly.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be repeated exactly.'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo)); } break; case '!=': if (($this->strict && $value === $compareValue) || (!$this->strict && $value == $compareValue)) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; case '>': if ($value <= $compareValue) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; case '>=': if ($value < $compareValue) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; case '<': if ($value >= $compareValue) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be less than "{compareValue}".'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be less than "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; case '<=': if ($value > $compareValue) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; @@ -129,8 +130,9 @@ class CompareValidator extends Validator /** * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @return string the client-side validation script. + * @param string $attribute the name of the attribute to be validated + * @return string the client-side validation script + * @throws \yii\base\Exception if CompareValidator::operator is invalid */ public function clientValidateAttribute($object, $attribute) { @@ -138,7 +140,7 @@ class CompareValidator extends Validator $compareTo = $this->compareValue; $compareValue = json_encode($this->compareValue); } else { - $compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; + $compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute; $compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()"; $compareTo = $object->getAttributeLabel($compareAttribute); } @@ -148,37 +150,37 @@ class CompareValidator extends Validator case '=': case '==': if ($message === null) { - $message = Yii::t('yii', '{attribute} must be repeated exactly.'); + $message = \Yii::t('yii', '{attribute} must be repeated exactly.'); } $condition = 'value!=' . $compareValue; break; case '!=': if ($message === null) { - $message = Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); + $message = \Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); } $condition = 'value==' . $compareValue; break; case '>': if ($message === null) { - $message = Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); + $message = \Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); } $condition = 'value<=' . $compareValue; break; case '>=': if ($message === null) { - $message = Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".'); + $message = \Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".'); } $condition = 'value<' . $compareValue; break; case '<': if ($message === null) { - $message = Yii::t('yii', '{attribute} must be less than "{compareValue}".'); + $message = \Yii::t('yii', '{attribute} must be less than "{compareValue}".'); } $condition = 'value>=' . $compareValue; break; case '<=': if ($message === null) { - $message = Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".'); + $message = \Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".'); } $condition = 'value>' . $compareValue; break; diff --git a/framework/validators/DateValidator.php b/framework/validators/DateValidator.php index f3309f2..7a352f1 100644 --- a/framework/validators/DateValidator.php +++ b/framework/validators/DateValidator.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CDateValidator extends Validator +class DateValidator extends Validator { /** * @var mixed the format pattern that the date value should follow. @@ -48,26 +48,25 @@ class CDateValidator extends Validator public function validateAttribute($object, $attribute) { $value = $object->$attribute; - if ($this->allowEmpty && $this->isEmpty($value)) + if ($this->allowEmpty && $this->isEmpty($value)) { return; + } $formats = is_string($this->format) ? array($this->format) : $this->format; $valid = false; - foreach ($formats as $format) - { + foreach ($formats as $format) { $timestamp = CDateTimeParser::parse($value, $format, array('month' => 1, 'day' => 1, 'hour' => 0, 'minute' => 0, 'second' => 0)); - if ($timestamp !== false) - { + if ($timestamp !== false) { $valid = true; - if ($this->timestampAttribute !== null) + if ($this->timestampAttribute !== null) { $object-> {$this->timestampAttribute} = $timestamp; + } break; } } - if (!$valid) - { - $message = $this->message !== null ? $this->message : Yii::t('yii', 'The format of {attribute} is invalid.'); + if (!$valid) { + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', 'The format of {attribute} is invalid.'); $this->addError($object, $attribute, $message); } } diff --git a/framework/validators/EmailValidator.php b/framework/validators/EmailValidator.php index 41d0d38..c316c61 100644 --- a/framework/validators/EmailValidator.php +++ b/framework/validators/EmailValidator.php @@ -63,7 +63,7 @@ class EmailValidator extends Validator return; } if (!$this->validateValue($value)) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid email address.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} is not a valid email address.'); $this->addError($object, $attribute, $message); } } @@ -99,7 +99,7 @@ class EmailValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid email address.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} is not a valid email address.'); $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, diff --git a/framework/validators/ExistValidator.php b/framework/validators/ExistValidator.php index 10f4e82..9ef86ff 100644 --- a/framework/validators/ExistValidator.php +++ b/framework/validators/ExistValidator.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CExistValidator extends Validator +class ExistValidator extends Validator { /** - * @var string the ActiveRecord class name that should be used to - * look for the attribute value being validated. Defaults to null, - * meaning using the ActiveRecord class of the attribute being validated. - * You may use path alias to reference a class name here. + * @var string the yii\db\ar\ActiveRecord class name or alias of the class + * that should be used to look for the attribute value being validated. + * Defaults to null, meaning using the yii\db\ar\ActiveRecord class of + * the attribute being validated. * @see attributeName */ public $className; /** - * @var string the ActiveRecord class attribute name that should be + * @var string the yii\db\ar\ActiveRecord class attribute name that should be * used to look for the attribute value being validated. Defaults to null, * meaning using the name of the attribute being validated. * @see className */ public $attributeName; /** - * @var array additional query criteria. This will be combined with the condition - * that checks if the attribute value exists in the corresponding table column. - * This array will be used to instantiate a {@link CDbCriteria} object. + * @var \yii\db\dao\BaseQuery additional query criteria. This will be combined + * with the condition that checks if the attribute value exists in the + * corresponding table column. */ - public $criteria = array(); + public $query = null; /** * @var boolean whether the attribute value can be null or empty. Defaults to true, * meaning that if the attribute is empty, it is considered valid. @@ -50,33 +50,34 @@ class CExistValidator extends Validator /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated + * + * @param \yii\db\ar\ActiveRecord $object the object being validated * @param string $attribute the attribute being validated + * + * @throws \yii\base\Exception if table doesn't have column specified */ public function validateAttribute($object, $attribute) { $value = $object->$attribute; - if ($this->allowEmpty && $this->isEmpty($value)) + if ($this->allowEmpty && $this->isEmpty($value)) { return; + } + + $className = ($this->className === null) ? get_class($object) : \Yii::import($this->className); + $attributeName = ($this->attributeName === null) ? $attribute : $this->attributeName; + $table = $object::getMetaData()->table; + if (($column = $table->getColumn($attributeName)) === null) { + throw new \yii\base\Exception('Table "' . $table->name . '" does not have a column named "' . $attributeName . '"'); + } - $className = $this->className === null ? get_class($object) : Yii::import($this->className); - $attributeName = $this->attributeName === null ? $attribute : $this->attributeName; - $finder = CActiveRecord::model($className); - $table = $finder->getTableSchema(); - if (($column = $table->getColumn($attributeName)) === null) - throw new CException(Yii::t('yii', 'Table "{table}" does not have a column named "{column}".', - array('{column}' => $attributeName, '{table}' => $table->name))); + $finder = $object->find()->where(array($column->name => $value)); - $criteria = array('condition' => $column->rawName . '=:vp', 'params' => array(':vp' => $value)); - if ($this->criteria !== array()) - { - $criteria = new CDbCriteria($criteria); - $criteria->mergeWith($this->criteria); + if ($this->query instanceof \yii\db\dao\BaseQuery) { + $finder->mergeWith($this->query); } - if (!$finder->exists($criteria)) - { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" is invalid.'); + if (!$finder->exists()) { + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} "{value}" is invalid.'); $this->addError($object, $attribute, $message, array('{value}' => $value)); } } diff --git a/framework/validators/FileValidator.php b/framework/validators/FileValidator.php index a1ce299..fec253b 100644 --- a/framework/validators/FileValidator.php +++ b/framework/validators/FileValidator.php @@ -115,7 +115,7 @@ class CFileValidator extends Validator return $this->emptyAttribute($object, $attribute); if (count($files) > $this->maxFiles) { - $message = $this->tooMany !== null ? $this->tooMany : Yii::t('yii', '{attribute} cannot accept more than {limit} files.'); + $message = $this->tooMany !== null ? $this->tooMany : \Yii::t('yii', '{attribute} cannot accept more than {limit} files.'); $this->addError($object, $attribute, $message, array('{attribute}' => $attribute, '{limit}' => $this->maxFiles)); } else foreach ($files as $file) @@ -145,20 +145,20 @@ class CFileValidator extends Validator return $this->emptyAttribute($object, $attribute); elseif ($error == UPLOAD_ERR_INI_SIZE || $error == UPLOAD_ERR_FORM_SIZE || $this->maxSize !== null && $file->getSize() > $this->maxSize) { - $message = $this->tooLarge !== null ? $this->tooLarge : Yii::t('yii', 'The file "{file}" is too large. Its size cannot exceed {limit} bytes.'); + $message = $this->tooLarge !== null ? $this->tooLarge : \Yii::t('yii', 'The file "{file}" is too large. Its size cannot exceed {limit} bytes.'); $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->getSizeLimit())); } elseif ($error == UPLOAD_ERR_PARTIAL) - throw new CException(Yii::t('yii', 'The file "{file}" was only partially uploaded.', array('{file}' => $file->getName()))); + throw new CException(\Yii::t('yii', 'The file "{file}" was only partially uploaded.', array('{file}' => $file->getName()))); elseif ($error == UPLOAD_ERR_NO_TMP_DIR) - throw new CException(Yii::t('yii', 'Missing the temporary folder to store the uploaded file "{file}".', array('{file}' => $file->getName()))); + throw new CException(\Yii::t('yii', 'Missing the temporary folder to store the uploaded file "{file}".', array('{file}' => $file->getName()))); elseif ($error == UPLOAD_ERR_CANT_WRITE) - throw new CException(Yii::t('yii', 'Failed to write the uploaded file "{file}" to disk.', array('{file}' => $file->getName()))); + throw new CException(\Yii::t('yii', 'Failed to write the uploaded file "{file}" to disk.', array('{file}' => $file->getName()))); elseif (defined('UPLOAD_ERR_EXTENSION') && $error == UPLOAD_ERR_EXTENSION) // available for PHP 5.2.0 or above - throw new CException(Yii::t('yii', 'File upload was stopped by extension.')); + throw new CException(\Yii::t('yii', 'File upload was stopped by extension.')); if ($this->minSize !== null && $file->getSize() < $this->minSize) { - $message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii', 'The file "{file}" is too small. Its size cannot be smaller than {limit} bytes.'); + $message = $this->tooSmall !== null ? $this->tooSmall : \Yii::t('yii', 'The file "{file}" is too small. Its size cannot be smaller than {limit} bytes.'); $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->minSize)); } @@ -170,7 +170,7 @@ class CFileValidator extends Validator $types = $this->types; if (!in_array(strtolower($file->getExtensionName()), $types)) { - $message = $this->wrongType !== null ? $this->wrongType : Yii::t('yii', 'The file "{file}" cannot be uploaded. Only files with these extensions are allowed: {extensions}.'); + $message = $this->wrongType !== null ? $this->wrongType : \Yii::t('yii', 'The file "{file}" cannot be uploaded. Only files with these extensions are allowed: {extensions}.'); $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{extensions}' => implode(', ', $types))); } } @@ -185,7 +185,7 @@ class CFileValidator extends Validator { if (!$this->allowEmpty) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.'); + $message = $this->message !== null ? $this->message : \Yii::t('yii', '{attribute} cannot be blank.'); $this->addError($object, $attribute, $message); } } diff --git a/framework/validators/FilterValidator.php b/framework/validators/FilterValidator.php index ac49c9d..88b20b9 100644 --- a/framework/validators/FilterValidator.php +++ b/framework/validators/FilterValidator.php @@ -45,6 +45,7 @@ class FilterValidator extends Validator * If there is any error, the error message is added to the object. * @param \yii\base\Model $object the object being validated * @param string $attribute the attribute being validated + * @throws \yii\base\Exception if filter property is not a valid callback */ public function validateAttribute($object, $attribute) { diff --git a/framework/validators/InlineValidator.php b/framework/validators/InlineValidator.php index e942d49..32dfb45 100644 --- a/framework/validators/InlineValidator.php +++ b/framework/validators/InlineValidator.php @@ -27,7 +27,8 @@ namespace yii\validators; class InlineValidator extends Validator { /** - * @var string the name of the validation method defined in the active record class + * @var string the name of the validation method defined in the + * \yii\base\Model class */ public $method; /** @@ -81,9 +82,9 @@ class InlineValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - if($this->clientValidate !== null) { + if ($this->clientValidate !== null) { $method = $this->clientValidate; return $object->$method($attribute); } } -} +} \ No newline at end of file diff --git a/framework/validators/IntegerValidator.php b/framework/validators/IntegerValidator.php index fcf33c1..b497af7 100644 --- a/framework/validators/IntegerValidator.php +++ b/framework/validators/IntegerValidator.php @@ -31,7 +31,7 @@ class IntegerValidator extends NumberValidator public function validateAttribute($object, $attribute) { if ($this->message === null) { - $this->message = Yii::t('yii', '{attribute} must be an integer.'); + $this->message = \Yii::t('yii', '{attribute} must be an integer.'); } parent::validateAttribute($object, $attribute); } @@ -45,7 +45,7 @@ class IntegerValidator extends NumberValidator public function clientValidateAttribute($object, $attribute) { if ($this->message === null) { - $this->message = Yii::t('yii', '{attribute} must be an integer.'); + $this->message = \Yii::t('yii', '{attribute} must be an integer.'); } return parent::clientValidateAttribute($object, $attribute); } diff --git a/framework/validators/NumberValidator.php b/framework/validators/NumberValidator.php index 25e5434..de71b31 100644 --- a/framework/validators/NumberValidator.php +++ b/framework/validators/NumberValidator.php @@ -62,15 +62,15 @@ class NumberValidator extends Validator return; } if (!preg_match($this->pattern, "$value")) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be a number.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be a number.'); $this->addError($object, $attribute, $message); } if ($this->min !== null && $value < $this->min) { - $message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii', '{attribute} is too small (minimum is {min}).'); + $message = ($this->tooSmall !== null) ? $this->tooSmall : \Yii::t('yii', '{attribute} is too small (minimum is {min}).'); $this->addError($object, $attribute, $message, array('{min}' => $this->min)); } if ($this->max !== null && $value > $this->max) { - $message = $this->tooBig !== null ? $this->tooBig : Yii::t('yii', '{attribute} is too big (maximum is {max}).'); + $message = ($this->tooBig !== null) ? $this->tooBig : \Yii::t('yii', '{attribute} is too big (maximum is {max}).'); $this->addError($object, $attribute, $message, array('{max}' => $this->max)); } } @@ -87,7 +87,7 @@ class NumberValidator extends Validator $value = $object->$attribute; if (($message = $this->message) === null) { - $message = Yii::t('yii', '{attribute} must be a number.'); + $message = \Yii::t('yii', '{attribute} must be a number.'); } $message = strtr($message, array( '{attribute}' => $label, @@ -95,7 +95,7 @@ class NumberValidator extends Validator )); if (($tooBig = $this->tooBig) === null) { - $tooBig = Yii::t('yii', '{attribute} is too big (maximum is {max}).'); + $tooBig = \Yii::t('yii', '{attribute} is too big (maximum is {max}).'); } $tooBig = strtr($tooBig, array( '{attribute}' => $label, @@ -104,7 +104,7 @@ class NumberValidator extends Validator )); if (($tooSmall = $this->tooSmall) === null) { - $tooSmall = Yii::t('yii', '{attribute} is too small (minimum is {min}).'); + $tooSmall = \Yii::t('yii', '{attribute} is too small (minimum is {min}).'); } $tooSmall = strtr($tooSmall, array( '{attribute}' => $label, diff --git a/framework/validators/RangeValidator.php b/framework/validators/RangeValidator.php index ab04dbb..662c08d 100644 --- a/framework/validators/RangeValidator.php +++ b/framework/validators/RangeValidator.php @@ -45,6 +45,7 @@ class RangeValidator extends Validator * If there is any error, the error message is added to the object. * @param \yii\base\Model $object the object being validated * @param string $attribute the attribute being validated + * @throws \yii\base\Exception if the "range" property is not an array */ public function validateAttribute($object, $attribute) { @@ -56,10 +57,10 @@ class RangeValidator extends Validator throw new \yii\base\Exception('The "range" property must be specified as an array.'); } if (!$this->not && !in_array($value, $this->range, $this->strict)) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} should be in the list.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} should be in the list.'); $this->addError($object, $attribute, $message); } elseif ($this->not && in_array($value, $this->range, $this->strict)) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} should NOT be in the list.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} should NOT be in the list.'); $this->addError($object, $attribute, $message); } } @@ -69,6 +70,7 @@ class RangeValidator extends Validator * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. + * @throws \yii\base\Exception if the "range" property is not an array */ public function clientValidateAttribute($object, $attribute) { @@ -77,7 +79,7 @@ class RangeValidator extends Validator } if (($message = $this->message) === null) { - $message = $this->not ? Yii::t('yii', '{attribute} should NOT be in the list.') : Yii::t('yii', '{attribute} should be in the list.'); + $message = $this->not ? \Yii::t('yii', '{attribute} should NOT be in the list.') : \Yii::t('yii', '{attribute} should be in the list.'); } $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), diff --git a/framework/validators/RegularExpressionValidator.php b/framework/validators/RegularExpressionValidator.php index a1630ab..26d1392 100644 --- a/framework/validators/RegularExpressionValidator.php +++ b/framework/validators/RegularExpressionValidator.php @@ -39,6 +39,7 @@ class RegularExpressionValidator extends Validator * If there is any error, the error message is added to the object. * @param \yii\base\Model $object the object being validated * @param string $attribute the attribute being validated + * @throws \yii\base\Exception if the "pattern" is not a valid regular expression */ public function validateAttribute($object, $attribute) { @@ -47,10 +48,10 @@ class RegularExpressionValidator extends Validator return; } if ($this->pattern === null) { - throw new \yii\base\Exception(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.')); + throw new \yii\base\Exception('The "pattern" property must be specified with a valid regular expression.'); } if ((!$this->not && !preg_match($this->pattern, $value)) || ($this->not && preg_match($this->pattern, $value))) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is invalid.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} is invalid.'); $this->addError($object, $attribute, $message); } } @@ -60,6 +61,7 @@ class RegularExpressionValidator extends Validator * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. + * @throws \yii\base\Exception if the "pattern" is not a valid regular expression */ public function clientValidateAttribute($object, $attribute) { @@ -67,7 +69,7 @@ class RegularExpressionValidator extends Validator throw new \yii\base\Exception('The "pattern" property must be specified with a valid regular expression.'); } - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is invalid.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} is invalid.'); $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, diff --git a/framework/validators/RequiredValidator.php b/framework/validators/RequiredValidator.php index 60677f6..c7f294c 100644 --- a/framework/validators/RequiredValidator.php +++ b/framework/validators/RequiredValidator.php @@ -47,12 +47,12 @@ class RequiredValidator extends Validator $value = $object->$attribute; if ($this->requiredValue === null) { if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} cannot be blank.'); $this->addError($object, $attribute, $message); } } else { if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be "{requiredValue}".', + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be "{requiredValue}".', array('{requiredValue}' => $this->requiredValue)); $this->addError($object, $attribute, $message); } @@ -70,7 +70,7 @@ class RequiredValidator extends Validator $message = $this->message; if ($this->requiredValue !== null) { if ($message === null) { - $message = Yii::t('yii', '{attribute} must be "{requiredValue}".'); + $message = \Yii::t('yii', '{attribute} must be "{requiredValue}".'); } $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), @@ -84,7 +84,7 @@ if (value != " . json_encode($this->requiredValue) . ") { "; } else { if ($message === null) { - $message = Yii::t('yii', '{attribute} cannot be blank.'); + $message = \Yii::t('yii', '{attribute} cannot be blank.'); } $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), diff --git a/framework/validators/StringValidator.php b/framework/validators/StringValidator.php index c5284f1..e46ee94 100644 --- a/framework/validators/StringValidator.php +++ b/framework/validators/StringValidator.php @@ -76,7 +76,7 @@ class StringValidator extends Validator } if (!is_string($value)) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be a string.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be a string.'); $this->addError($object, $attribute, $message); return; } @@ -88,15 +88,15 @@ class StringValidator extends Validator } if ($this->min !== null && $length < $this->min) { - $message = $this->tooShort !== null ? $this->tooShort : Yii::t('yii', '{attribute} is too short (minimum is {min} characters).'); + $message = ($this->tooShort !== null) ? $this->tooShort : \Yii::t('yii', '{attribute} is too short (minimum is {min} characters).'); $this->addError($object, $attribute, $message, array('{min}' => $this->min)); } if ($this->max !== null && $length > $this->max) { - $message = $this->tooLong !== null ? $this->tooLong : Yii::t('yii', '{attribute} is too long (maximum is {max} characters).'); + $message = ($this->tooLong !== null) ? $this->tooLong : \Yii::t('yii', '{attribute} is too long (maximum is {max} characters).'); $this->addError($object, $attribute, $message, array('{max}' => $this->max)); } if ($this->is !== null && $length !== $this->is) { - $message = $this->notEqual !== null ? $this->notEqual : Yii::t('yii', '{attribute} is of the wrong length (should be {length} characters).'); + $message = ($this->notEqual !== null) ? $this->notEqual : \Yii::t('yii', '{attribute} is of the wrong length (should be {length} characters).'); $this->addError($object, $attribute, $message, array('{length}' => $this->is)); } } @@ -113,7 +113,7 @@ class StringValidator extends Validator $value = $object->$attribute; if (($notEqual = $this->notEqual) === null) { - $notEqual = Yii::t('yii', '{attribute} is of the wrong length (should be {length} characters).'); + $notEqual = \Yii::t('yii', '{attribute} is of the wrong length (should be {length} characters).'); } $notEqual = strtr($notEqual, array( '{attribute}' => $label, @@ -122,7 +122,7 @@ class StringValidator extends Validator )); if (($tooShort = $this->tooShort) === null) { - $tooShort = Yii::t('yii', '{attribute} is too short (minimum is {min} characters).'); + $tooShort = \Yii::t('yii', '{attribute} is too short (minimum is {min} characters).'); } $tooShort = strtr($tooShort, array( '{attribute}' => $label, @@ -131,7 +131,7 @@ class StringValidator extends Validator )); if (($tooLong = $this->tooLong) === null) { - $tooLong = Yii::t('yii', '{attribute} is too long (maximum is {max} characters).'); + $tooLong = \Yii::t('yii', '{attribute} is too long (maximum is {max} characters).'); } $tooLong = strtr($tooLong, array( '{attribute}' => $label, diff --git a/framework/validators/UniqueValidator.php b/framework/validators/UniqueValidator.php index c58338b..0366792 100644 --- a/framework/validators/UniqueValidator.php +++ b/framework/validators/UniqueValidator.php @@ -1,6 +1,6 @@ - * @since 1.0 + * @since 2.0 */ -class CUniqueValidator extends Validator +class UniqueValidator extends Validator { /** * @var boolean whether the comparison is case sensitive. Defaults to true. @@ -28,10 +28,10 @@ class CUniqueValidator extends Validator */ public $allowEmpty = true; /** - * @var string the ActiveRecord class name that should be used to - * look for the attribute value being validated. Defaults to null, meaning using - * the class of the object currently being validated. - * You may use path alias to reference a class name here. + * @var string the yii\db\ar\ActiveRecord class name or alias of the class + * that should be used to look for the attribute value being validated. + * Defaults to null, meaning using the class of the object currently + * being validated. * @see attributeName */ public $className; @@ -43,11 +43,11 @@ class CUniqueValidator extends Validator */ public $attributeName; /** - * @var array additional query criteria. This will be combined with the condition - * that checks if the attribute value exists in the corresponding table column. - * This array will be used to instantiate a {@link CDbCriteria} object. + * @var \yii\db\ar\ActiveQuery additional query criteria. This will be + * combined with the condition that checks if the attribute value exists + * in the corresponding table column. */ - public $criteria = array(); + public $query = null; /** * @var string the user-defined error message. The placeholders "{attribute}" and "{value}" * are recognized, which will be replaced with the actual attribute name and value, respectively. @@ -63,56 +63,60 @@ class CUniqueValidator extends Validator /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated + * @param \yiiunit\data\ar\ActiveRecord $object the object being validated * @param string $attribute the attribute being validated + * + * @throws \yii\base\Exception if table doesn't have column specified */ public function validateAttribute($object, $attribute) { $value = $object->$attribute; - if ($this->allowEmpty && $this->isEmpty($value)) + if ($this->allowEmpty && $this->isEmpty($value)) { return; + } + + $className = ($this->className === null) ? get_class($object) : \Yii::import($this->className); + $attributeName = ($this->attributeName === null) ? $attribute : $this->attributeName; - $className = $this->className === null ? get_class($object) : Yii::import($this->className); - $attributeName = $this->attributeName === null ? $attribute : $this->attributeName; - $finder = CActiveRecord::model($className); - $table = $finder->getTableSchema(); - if (($column = $table->getColumn($attributeName)) === null) - throw new CException(Yii::t('yii', 'Table "{table}" does not have a column named "{column}".', - array('{column}' => $attributeName, '{table}' => $table->name))); + $table = $object::getMetaData()->table; + if (($column = $table->getColumn($attributeName)) === null) { + throw new \yii\base\Exception('Table "' . $table->name . '" does not have a column named "' . $attributeName . '"'); + } - $columnName = $column->rawName; - $criteria = new CDbCriteria(array( - 'condition' => $this->caseSensitive ? "$columnName=:value" : "LOWER($columnName)=LOWER(:value)", - 'params' => array(':value' => $value), - )); - if ($this->criteria !== array()) - $criteria->mergeWith($this->criteria); + $finder = $object::find(); + $finder->where($this->caseSensitive ? "{$column->quotedName}=:value" : "LOWER({$column->quotedName})=LOWER(:value)"); + $finder->params(array(':value' => $value)); + + if ($this->query instanceof \yii\db\dao\BaseQuery) { + $finder->mergeWith($this->query); + } + + if ($object->getIsNewRecord()) { + // if current $object isn't in the database yet then it's OK just + // to call exists() + $exists = $finder->exists(); + } else { + // if current $object is in the database already we can't use exists() + $finder->limit(2); + $objects = $finder->all(); - if (!$object instanceof CActiveRecord || $object->isNewRecord || $object->tableName() !== $finder->tableName()) - $exists = $finder->exists($criteria); - else - { - $criteria->limit = 2; - $objects = $finder->findAll($criteria); $n = count($objects); - if ($n === 1) - { - if ($column->isPrimaryKey) // primary key is modified and not unique + if ($n === 1) { + if ($column->isPrimaryKey) { + // primary key is modified and not unique $exists = $object->getOldPrimaryKey() != $object->getPrimaryKey(); - else - { + } else { // non-primary key, need to exclude the current record based on PK $exists = array_shift($objects)->getPrimaryKey() != $object->getOldPrimaryKey(); } - } else + } else { $exists = $n > 1; + } } - if ($exists) - { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" has already been taken.'); + if ($exists) { + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} "{value}" has already been taken.'); $this->addError($object, $attribute, $message, array('{value}' => $value)); } } -} - +} \ No newline at end of file diff --git a/framework/validators/UrlValidator.php b/framework/validators/UrlValidator.php index 52412e7..d3fe679 100644 --- a/framework/validators/UrlValidator.php +++ b/framework/validators/UrlValidator.php @@ -55,7 +55,7 @@ class UrlValidator extends Validator if (($value = $this->validateValue($value)) !== false) { $object->$attribute = $value; } else { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid URL.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} is not a valid URL.'); $this->addError($object, $attribute, $message); } } @@ -97,24 +97,24 @@ class UrlValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid URL.'); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} is not a valid URL.'); $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, )); - if (strpos($this->pattern, '{schemes}') !== false) + if (strpos($this->pattern, '{schemes}') !== false) { $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern); - else + } else { $pattern = $this->pattern; + } $js = " if(!value.match($pattern)) { messages.push(" . json_encode($message) . "); } "; - if ($this->defaultScheme !== null) - { + if ($this->defaultScheme !== null) { $js = " if(!value.match(/:\\/\\//)) { value=" . json_encode($this->defaultScheme) . "+'://'+value; @@ -123,8 +123,7 @@ $js "; } - if ($this->allowEmpty) - { + if ($this->allowEmpty) { $js = " if($.trim(value)!='') { $js diff --git a/framework/validators/Validator.php b/framework/validators/Validator.php index aa0d8e6..7e90e46 100644 --- a/framework/validators/Validator.php +++ b/framework/validators/Validator.php @@ -69,7 +69,6 @@ abstract class Validator extends \yii\base\Component 'integer' => '\yii\validators\IntegerValidator', 'double' => '\yii\validators\NumberValidator', 'compare' => '\yii\validators\CompareValidator', - 'file' => '\yii\validators\FileValidator', 'date' => '\yii\validators\DateValidator', 'unique' => '\yii\validators\UniqueValidator', @@ -145,7 +144,8 @@ abstract class Validator extends \yii\base\Component $params['on'] = array(); } - if (method_exists($object, $type)) { // method-based validator + if (method_exists($object, $type)) { + // method-based validator $config = array( 'class' => '\yii\validators\InlineValidator', 'method' => $type,