Browse Source

validators

tags/2.0.0-beta
Alexander Makarov 13 years ago
parent
commit
083ca919d3
  1. 4
      framework/validators/BooleanValidator.php
  2. 4
      framework/validators/CaptchaValidator.php
  3. 34
      framework/validators/CompareValidator.php
  4. 23
      framework/validators/DateValidator.php
  5. 4
      framework/validators/EmailValidator.php
  6. 59
      framework/validators/ExistValidator.php
  7. 18
      framework/validators/FileValidator.php
  8. 1
      framework/validators/FilterValidator.php
  9. 5
      framework/validators/InlineValidator.php
  10. 4
      framework/validators/IntegerValidator.php
  11. 12
      framework/validators/NumberValidator.php
  12. 8
      framework/validators/RangeValidator.php
  13. 8
      framework/validators/RegularExpressionValidator.php
  14. 8
      framework/validators/RequiredValidator.php
  15. 14
      framework/validators/StringValidator.php
  16. 90
      framework/validators/UniqueValidator.php
  17. 15
      framework/validators/UrlValidator.php
  18. 4
      framework/validators/Validator.php

4
framework/validators/BooleanValidator.php

@ -54,7 +54,7 @@ class BooleanValidator extends Validator
} }
if (!$this->strict && $value != $this->trueValue && $value != $this->falseValue if (!$this->strict && $value != $this->trueValue && $value != $this->falseValue
|| $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( $this->addError($object, $attribute, $message, array(
'{true}' => $this->trueValue, '{true}' => $this->trueValue,
'{false}' => $this->falseValue, '{false}' => $this->falseValue,
@ -70,7 +70,7 @@ class BooleanValidator extends Validator
*/ */
public function clientValidateAttribute($object, $attribute) 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( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{value}' => $object->$attribute,

4
framework/validators/CaptchaValidator.php

@ -49,7 +49,7 @@ class CaptchaValidator extends Validator
} }
$captcha = $this->getCaptchaAction(); $captcha = $this->getCaptchaAction();
if (!$captcha->validate($value, $this->caseSensitive)) { 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); $this->addError($object, $attribute, $message);
} }
} }
@ -85,7 +85,7 @@ class CaptchaValidator extends Validator
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
$captcha = $this->getCaptchaAction(); $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( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{value}' => $object->$attribute,

34
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. * If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
* @throws \yii\base\Exception if CompareValidator::operator is invalid
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
@ -78,7 +79,7 @@ class CompareValidator extends Validator
if ($this->compareValue !== null) { if ($this->compareValue !== null) {
$compareTo = $compareValue = $this->compareValue; $compareTo = $compareValue = $this->compareValue;
} else { } else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; $compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute;
$compareValue = $object->$compareAttribute; $compareValue = $object->$compareAttribute;
$compareTo = $object->getAttributeLabel($compareAttribute); $compareTo = $object->getAttributeLabel($compareAttribute);
} }
@ -87,37 +88,37 @@ class CompareValidator extends Validator
case '=': case '=':
case '==': case '==':
if (($this->strict && $value !== $compareValue) || (!$this->strict && $value != $compareValue)) { 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)); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo));
} }
break; break;
case '!=': case '!=':
if (($this->strict && $value === $compareValue) || (!$this->strict && $value == $compareValue)) { 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)); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
} }
break; break;
case '>': case '>':
if ($value <= $compareValue) { 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)); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
} }
break; break;
case '>=': case '>=':
if ($value < $compareValue) { 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)); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
} }
break; break;
case '<': case '<':
if ($value >= $compareValue) { 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)); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
} }
break; break;
case '<=': case '<=':
if ($value > $compareValue) { 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)); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
} }
break; break;
@ -129,8 +130,9 @@ class CompareValidator extends Validator
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated
* @return string the client-side validation script. * @return string the client-side validation script
* @throws \yii\base\Exception if CompareValidator::operator is invalid
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
@ -138,7 +140,7 @@ class CompareValidator extends Validator
$compareTo = $this->compareValue; $compareTo = $this->compareValue;
$compareValue = json_encode($this->compareValue); $compareValue = json_encode($this->compareValue);
} else { } else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; $compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute;
$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()"; $compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()";
$compareTo = $object->getAttributeLabel($compareAttribute); $compareTo = $object->getAttributeLabel($compareAttribute);
} }
@ -148,37 +150,37 @@ class CompareValidator extends Validator
case '=': case '=':
case '==': case '==':
if ($message === null) { if ($message === null) {
$message = Yii::t('yii', '{attribute} must be repeated exactly.'); $message = \Yii::t('yii', '{attribute} must be repeated exactly.');
} }
$condition = 'value!=' . $compareValue; $condition = 'value!=' . $compareValue;
break; break;
case '!=': case '!=':
if ($message === null) { 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; $condition = 'value==' . $compareValue;
break; break;
case '>': case '>':
if ($message === null) { 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; $condition = 'value<=' . $compareValue;
break; break;
case '>=': case '>=':
if ($message === null) { 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; $condition = 'value<' . $compareValue;
break; break;
case '<': case '<':
if ($message === null) { 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; $condition = 'value>=' . $compareValue;
break; break;
case '<=': case '<=':
if ($message === null) { 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; $condition = 'value>' . $compareValue;
break; break;

23
framework/validators/DateValidator.php

@ -1,6 +1,6 @@
<?php <?php
/** /**
* CDateValidator class file. * DateValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,7 +10,7 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CDateValidator verifies if the attribute represents a date, time or datetime. * DateValidator verifies if the attribute represents a date, time or datetime.
* *
* By setting the {@link format} property, one can specify what format the date value * By setting the {@link format} property, one can specify what format the date value
* must be in. If the given date value doesn't follow the format, the attribute is considered as invalid. * must be in. If the given date value doesn't follow the format, the attribute is considered as invalid.
@ -18,7 +18,7 @@ namespace yii\validators;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class CDateValidator extends Validator class DateValidator extends Validator
{ {
/** /**
* @var mixed the format pattern that the date value should follow. * @var mixed the format pattern that the date value should follow.
@ -48,26 +48,25 @@ class CDateValidator extends Validator
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value)) {
return; return;
}
$formats = is_string($this->format) ? array($this->format) : $this->format; $formats = is_string($this->format) ? array($this->format) : $this->format;
$valid = false; $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)); $timestamp = CDateTimeParser::parse($value, $format, array('month' => 1, 'day' => 1, 'hour' => 0, 'minute' => 0, 'second' => 0));
if ($timestamp !== false) if ($timestamp !== false) {
{
$valid = true; $valid = true;
if ($this->timestampAttribute !== null) if ($this->timestampAttribute !== null) {
$object-> {$this->timestampAttribute} = $timestamp; $object-> {$this->timestampAttribute} = $timestamp;
}
break; break;
} }
} }
if (!$valid) if (!$valid) {
{ $message = ($this->message !== null) ? $this->message : \Yii::t('yii', 'The format of {attribute} is invalid.');
$message = $this->message !== null ? $this->message : Yii::t('yii', 'The format of {attribute} is invalid.');
$this->addError($object, $attribute, $message); $this->addError($object, $attribute, $message);
} }
} }

4
framework/validators/EmailValidator.php

@ -63,7 +63,7 @@ class EmailValidator extends Validator
return; return;
} }
if (!$this->validateValue($value)) { 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); $this->addError($object, $attribute, $message);
} }
} }
@ -99,7 +99,7 @@ class EmailValidator extends Validator
*/ */
public function clientValidateAttribute($object, $attribute) 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( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{value}' => $object->$attribute,

59
framework/validators/ExistValidator.php

@ -1,6 +1,6 @@
<?php <?php
/** /**
* CExistValidator class file. * ExistValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,7 +10,7 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CExistValidator validates that the attribute value exists in a table. * ExistValidator validates that the attribute value exists in a table.
* *
* This validator is often used to verify that a foreign key contains a value * This validator is often used to verify that a foreign key contains a value
* that can be found in the foreign table. * that can be found in the foreign table.
@ -18,29 +18,29 @@ namespace yii\validators;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class CExistValidator extends Validator class ExistValidator extends Validator
{ {
/** /**
* @var string the ActiveRecord class name that should be used to * @var string the yii\db\ar\ActiveRecord class name or alias of the class
* look for the attribute value being validated. Defaults to null, * that should be used to look for the attribute value being validated.
* meaning using the ActiveRecord class of the attribute being validated. * Defaults to null, meaning using the yii\db\ar\ActiveRecord class of
* You may use path alias to reference a class name here. * the attribute being validated.
* @see attributeName * @see attributeName
*/ */
public $className; 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, * used to look for the attribute value being validated. Defaults to null,
* meaning using the name of the attribute being validated. * meaning using the name of the attribute being validated.
* @see className * @see className
*/ */
public $attributeName; public $attributeName;
/** /**
* @var array additional query criteria. This will be combined with the condition * @var \yii\db\dao\BaseQuery additional query criteria. This will be combined
* that checks if the attribute value exists in the corresponding table column. * with the condition that checks if the attribute value exists in the
* This array will be used to instantiate a {@link CDbCriteria} object. * corresponding table column.
*/ */
public $criteria = array(); public $query = null;
/** /**
* @var boolean whether the attribute value can be null or empty. Defaults to true, * @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. * 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. * Validates the attribute of the object.
* If there is any error, the error message is added to 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 * @param string $attribute the attribute being validated
*
* @throws \yii\base\Exception if table doesn't have column specified
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value)) {
return; return;
}
$className = $this->className === null ? get_class($object) : Yii::import($this->className); $className = ($this->className === null) ? get_class($object) : \Yii::import($this->className);
$attributeName = $this->attributeName === null ? $attribute : $this->attributeName; $attributeName = ($this->attributeName === null) ? $attribute : $this->attributeName;
$finder = CActiveRecord::model($className); $table = $object::getMetaData()->table;
$table = $finder->getTableSchema(); if (($column = $table->getColumn($attributeName)) === null) {
if (($column = $table->getColumn($attributeName)) === null) throw new \yii\base\Exception('Table "' . $table->name . '" does not have a column named "' . $attributeName . '"');
throw new CException(Yii::t('yii', 'Table "{table}" does not have a column named "{column}".', }
array('{column}' => $attributeName, '{table}' => $table->name)));
$criteria = array('condition' => $column->rawName . '=:vp', 'params' => array(':vp' => $value)); $finder = $object->find()->where(array($column->name => $value));
if ($this->criteria !== array())
{ if ($this->query instanceof \yii\db\dao\BaseQuery) {
$criteria = new CDbCriteria($criteria); $finder->mergeWith($this->query);
$criteria->mergeWith($this->criteria);
} }
if (!$finder->exists($criteria)) if (!$finder->exists()) {
{ $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} "{value}" is invalid.');
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" is invalid.');
$this->addError($object, $attribute, $message, array('{value}' => $value)); $this->addError($object, $attribute, $message, array('{value}' => $value));
} }
} }

18
framework/validators/FileValidator.php

@ -115,7 +115,7 @@ class CFileValidator extends Validator
return $this->emptyAttribute($object, $attribute); return $this->emptyAttribute($object, $attribute);
if (count($files) > $this->maxFiles) 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)); $this->addError($object, $attribute, $message, array('{attribute}' => $attribute, '{limit}' => $this->maxFiles));
} else } else
foreach ($files as $file) foreach ($files as $file)
@ -145,20 +145,20 @@ class CFileValidator extends Validator
return $this->emptyAttribute($object, $attribute); return $this->emptyAttribute($object, $attribute);
elseif ($error == UPLOAD_ERR_INI_SIZE || $error == UPLOAD_ERR_FORM_SIZE || $this->maxSize !== null && $file->getSize() > $this->maxSize) 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())); $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->getSizeLimit()));
} elseif ($error == UPLOAD_ERR_PARTIAL) } 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) 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) 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 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) 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)); $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->minSize));
} }
@ -170,7 +170,7 @@ class CFileValidator extends Validator
$types = $this->types; $types = $this->types;
if (!in_array(strtolower($file->getExtensionName()), $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))); $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{extensions}' => implode(', ', $types)));
} }
} }
@ -185,7 +185,7 @@ class CFileValidator extends Validator
{ {
if (!$this->allowEmpty) 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); $this->addError($object, $attribute, $message);
} }
} }

1
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. * If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute 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) public function validateAttribute($object, $attribute)
{ {

5
framework/validators/InlineValidator.php

@ -27,7 +27,8 @@ namespace yii\validators;
class InlineValidator extends Validator 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; public $method;
/** /**
@ -81,7 +82,7 @@ class InlineValidator extends Validator
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
if($this->clientValidate !== null) { if ($this->clientValidate !== null) {
$method = $this->clientValidate; $method = $this->clientValidate;
return $object->$method($attribute); return $object->$method($attribute);
} }

4
framework/validators/IntegerValidator.php

@ -31,7 +31,7 @@ class IntegerValidator extends NumberValidator
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
if ($this->message === null) { 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); parent::validateAttribute($object, $attribute);
} }
@ -45,7 +45,7 @@ class IntegerValidator extends NumberValidator
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
if ($this->message === null) { 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); return parent::clientValidateAttribute($object, $attribute);
} }

12
framework/validators/NumberValidator.php

@ -62,15 +62,15 @@ class NumberValidator extends Validator
return; return;
} }
if (!preg_match($this->pattern, "$value")) { 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); $this->addError($object, $attribute, $message);
} }
if ($this->min !== null && $value < $this->min) { 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)); $this->addError($object, $attribute, $message, array('{min}' => $this->min));
} }
if ($this->max !== null && $value > $this->max) { 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)); $this->addError($object, $attribute, $message, array('{max}' => $this->max));
} }
} }
@ -87,7 +87,7 @@ class NumberValidator extends Validator
$value = $object->$attribute; $value = $object->$attribute;
if (($message = $this->message) === null) { 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( $message = strtr($message, array(
'{attribute}' => $label, '{attribute}' => $label,
@ -95,7 +95,7 @@ class NumberValidator extends Validator
)); ));
if (($tooBig = $this->tooBig) === null) { 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( $tooBig = strtr($tooBig, array(
'{attribute}' => $label, '{attribute}' => $label,
@ -104,7 +104,7 @@ class NumberValidator extends Validator
)); ));
if (($tooSmall = $this->tooSmall) === null) { 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( $tooSmall = strtr($tooSmall, array(
'{attribute}' => $label, '{attribute}' => $label,

8
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. * If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute 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) 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.'); throw new \yii\base\Exception('The "range" property must be specified as an array.');
} }
if (!$this->not && !in_array($value, $this->range, $this->strict)) { 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); $this->addError($object, $attribute, $message);
} elseif ($this->not && in_array($value, $this->range, $this->strict)) { } 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); $this->addError($object, $attribute, $message);
} }
} }
@ -69,6 +70,7 @@ class RangeValidator extends Validator
* @param \yii\base\Model $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @throws \yii\base\Exception if the "range" property is not an array
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
@ -77,7 +79,7 @@ class RangeValidator extends Validator
} }
if (($message = $this->message) === null) { 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( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),

8
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. * If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute 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) public function validateAttribute($object, $attribute)
{ {
@ -47,10 +48,10 @@ class RegularExpressionValidator extends Validator
return; return;
} }
if ($this->pattern === null) { 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))) { 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); $this->addError($object, $attribute, $message);
} }
} }
@ -60,6 +61,7 @@ class RegularExpressionValidator extends Validator
* @param \yii\base\Model $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @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) 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.'); 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( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{value}' => $object->$attribute,

8
framework/validators/RequiredValidator.php

@ -47,12 +47,12 @@ class RequiredValidator extends Validator
$value = $object->$attribute; $value = $object->$attribute;
if ($this->requiredValue === null) { if ($this->requiredValue === null) {
if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) { 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); $this->addError($object, $attribute, $message);
} }
} else { } else {
if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) { 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)); array('{requiredValue}' => $this->requiredValue));
$this->addError($object, $attribute, $message); $this->addError($object, $attribute, $message);
} }
@ -70,7 +70,7 @@ class RequiredValidator extends Validator
$message = $this->message; $message = $this->message;
if ($this->requiredValue !== null) { if ($this->requiredValue !== null) {
if ($message === null) { if ($message === null) {
$message = Yii::t('yii', '{attribute} must be "{requiredValue}".'); $message = \Yii::t('yii', '{attribute} must be "{requiredValue}".');
} }
$message = strtr($message, array( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
@ -84,7 +84,7 @@ if (value != " . json_encode($this->requiredValue) . ") {
"; ";
} else { } else {
if ($message === null) { if ($message === null) {
$message = Yii::t('yii', '{attribute} cannot be blank.'); $message = \Yii::t('yii', '{attribute} cannot be blank.');
} }
$message = strtr($message, array( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),

14
framework/validators/StringValidator.php

@ -76,7 +76,7 @@ class StringValidator extends Validator
} }
if (!is_string($value)) { 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); $this->addError($object, $attribute, $message);
return; return;
} }
@ -88,15 +88,15 @@ class StringValidator extends Validator
} }
if ($this->min !== null && $length < $this->min) { 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)); $this->addError($object, $attribute, $message, array('{min}' => $this->min));
} }
if ($this->max !== null && $length > $this->max) { 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)); $this->addError($object, $attribute, $message, array('{max}' => $this->max));
} }
if ($this->is !== null && $length !== $this->is) { 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)); $this->addError($object, $attribute, $message, array('{length}' => $this->is));
} }
} }
@ -113,7 +113,7 @@ class StringValidator extends Validator
$value = $object->$attribute; $value = $object->$attribute;
if (($notEqual = $this->notEqual) === null) { 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( $notEqual = strtr($notEqual, array(
'{attribute}' => $label, '{attribute}' => $label,
@ -122,7 +122,7 @@ class StringValidator extends Validator
)); ));
if (($tooShort = $this->tooShort) === null) { 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( $tooShort = strtr($tooShort, array(
'{attribute}' => $label, '{attribute}' => $label,
@ -131,7 +131,7 @@ class StringValidator extends Validator
)); ));
if (($tooLong = $this->tooLong) === null) { 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( $tooLong = strtr($tooLong, array(
'{attribute}' => $label, '{attribute}' => $label,

90
framework/validators/UniqueValidator.php

@ -1,6 +1,6 @@
<?php <?php
/** /**
* CUniqueValidator class file. * UniqueValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -13,9 +13,9 @@ namespace yii\validators;
* CUniqueValidator validates that the attribute value is unique in the corresponding database table. * CUniqueValidator validates that the attribute value is unique in the corresponding database table.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @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. * @var boolean whether the comparison is case sensitive. Defaults to true.
@ -28,10 +28,10 @@ class CUniqueValidator extends Validator
*/ */
public $allowEmpty = true; public $allowEmpty = true;
/** /**
* @var string the ActiveRecord class name that should be used to * @var string the yii\db\ar\ActiveRecord class name or alias of the class
* look for the attribute value being validated. Defaults to null, meaning using * that should be used to look for the attribute value being validated.
* the class of the object currently being validated. * Defaults to null, meaning using the class of the object currently
* You may use path alias to reference a class name here. * being validated.
* @see attributeName * @see attributeName
*/ */
public $className; public $className;
@ -43,11 +43,11 @@ class CUniqueValidator extends Validator
*/ */
public $attributeName; public $attributeName;
/** /**
* @var array additional query criteria. This will be combined with the condition * @var \yii\db\ar\ActiveQuery additional query criteria. This will be
* that checks if the attribute value exists in the corresponding table column. * combined with the condition that checks if the attribute value exists
* This array will be used to instantiate a {@link CDbCriteria} object. * in the corresponding table column.
*/ */
public $criteria = array(); public $query = null;
/** /**
* @var string the user-defined error message. The placeholders "{attribute}" and "{value}" * @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. * 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. * Validates the attribute of the object.
* If there is any error, the error message is added to 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 * @param string $attribute the attribute being validated
*
* @throws \yii\base\Exception if table doesn't have column specified
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value)) {
return; return;
}
$className = $this->className === null ? get_class($object) : Yii::import($this->className); $className = ($this->className === null) ? get_class($object) : \Yii::import($this->className);
$attributeName = $this->attributeName === null ? $attribute : $this->attributeName; $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)));
$columnName = $column->rawName; $table = $object::getMetaData()->table;
$criteria = new CDbCriteria(array( if (($column = $table->getColumn($attributeName)) === null) {
'condition' => $this->caseSensitive ? "$columnName=:value" : "LOWER($columnName)=LOWER(:value)", throw new \yii\base\Exception('Table "' . $table->name . '" does not have a column named "' . $attributeName . '"');
'params' => array(':value' => $value), }
));
if ($this->criteria !== array()) $finder = $object::find();
$criteria->mergeWith($this->criteria); $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); $n = count($objects);
if ($n === 1) if ($n === 1) {
{ if ($column->isPrimaryKey) {
if ($column->isPrimaryKey) // primary key is modified and not unique // primary key is modified and not unique
$exists = $object->getOldPrimaryKey() != $object->getPrimaryKey(); $exists = $object->getOldPrimaryKey() != $object->getPrimaryKey();
else } else {
{
// non-primary key, need to exclude the current record based on PK // non-primary key, need to exclude the current record based on PK
$exists = array_shift($objects)->getPrimaryKey() != $object->getOldPrimaryKey(); $exists = array_shift($objects)->getPrimaryKey() != $object->getOldPrimaryKey();
} }
} else } else {
$exists = $n > 1; $exists = $n > 1;
} }
}
if ($exists) if ($exists) {
{ $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} "{value}" has already been taken.');
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" has already been taken.');
$this->addError($object, $attribute, $message, array('{value}' => $value)); $this->addError($object, $attribute, $message, array('{value}' => $value));
} }
} }
} }

15
framework/validators/UrlValidator.php

@ -55,7 +55,7 @@ class UrlValidator extends Validator
if (($value = $this->validateValue($value)) !== false) { if (($value = $this->validateValue($value)) !== false) {
$object->$attribute = $value; $object->$attribute = $value;
} else { } 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); $this->addError($object, $attribute, $message);
} }
} }
@ -97,24 +97,24 @@ class UrlValidator extends Validator
*/ */
public function clientValidateAttribute($object, $attribute) 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( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$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); $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
else } else {
$pattern = $this->pattern; $pattern = $this->pattern;
}
$js = " $js = "
if(!value.match($pattern)) { if(!value.match($pattern)) {
messages.push(" . json_encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
if ($this->defaultScheme !== null) if ($this->defaultScheme !== null) {
{
$js = " $js = "
if(!value.match(/:\\/\\//)) { if(!value.match(/:\\/\\//)) {
value=" . json_encode($this->defaultScheme) . "+'://'+value; value=" . json_encode($this->defaultScheme) . "+'://'+value;
@ -123,8 +123,7 @@ $js
"; ";
} }
if ($this->allowEmpty) if ($this->allowEmpty) {
{
$js = " $js = "
if($.trim(value)!='') { if($.trim(value)!='') {
$js $js

4
framework/validators/Validator.php

@ -69,7 +69,6 @@ abstract class Validator extends \yii\base\Component
'integer' => '\yii\validators\IntegerValidator', 'integer' => '\yii\validators\IntegerValidator',
'double' => '\yii\validators\NumberValidator', 'double' => '\yii\validators\NumberValidator',
'compare' => '\yii\validators\CompareValidator', 'compare' => '\yii\validators\CompareValidator',
'file' => '\yii\validators\FileValidator', 'file' => '\yii\validators\FileValidator',
'date' => '\yii\validators\DateValidator', 'date' => '\yii\validators\DateValidator',
'unique' => '\yii\validators\UniqueValidator', 'unique' => '\yii\validators\UniqueValidator',
@ -145,7 +144,8 @@ abstract class Validator extends \yii\base\Component
$params['on'] = array(); $params['on'] = array();
} }
if (method_exists($object, $type)) { // method-based validator if (method_exists($object, $type)) {
// method-based validator
$config = array( $config = array(
'class' => '\yii\validators\InlineValidator', 'class' => '\yii\validators\InlineValidator',
'method' => $type, 'method' => $type,

Loading…
Cancel
Save