From 421e31ec0fcf304a93cd79401b5b0798225158ad Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 4 Apr 2013 10:57:01 -0400 Subject: [PATCH] finished validator refactoring. --- framework/validators/CaptchaValidator.php | 2 +- framework/validators/ExistValidator.php | 8 ++++++++ framework/validators/NumberValidator.php | 4 ++++ framework/validators/RegularExpressionValidator.php | 9 +++++---- framework/validators/UniqueValidator.php | 6 ++++++ framework/validators/Validator.php | 2 +- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/framework/validators/CaptchaValidator.php b/framework/validators/CaptchaValidator.php index f35b332..65e7fd3 100644 --- a/framework/validators/CaptchaValidator.php +++ b/framework/validators/CaptchaValidator.php @@ -53,7 +53,7 @@ class CaptchaValidator extends Validator public function validateValue($value) { $captcha = $this->getCaptchaAction(); - return $captcha->validate($value, $this->caseSensitive); + return !is_array($value) && $captcha->validate($value, $this->caseSensitive); } /** diff --git a/framework/validators/ExistValidator.php b/framework/validators/ExistValidator.php index b39be56..ec01134 100644 --- a/framework/validators/ExistValidator.php +++ b/framework/validators/ExistValidator.php @@ -48,6 +48,11 @@ class ExistValidator extends Validator { $value = $object->$attribute; + if (is_array($value)) { + $this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.')); + return; + } + /** @var $className \yii\db\ActiveRecord */ $className = $this->className === null ? get_class($object) : Yii::import($this->className); $attributeName = $this->attributeName === null ? $attribute : $this->attributeName; @@ -67,6 +72,9 @@ class ExistValidator extends Validator */ public function validateValue($value) { + if (is_array($value)) { + return false; + } if ($this->className === null) { throw new InvalidConfigException('The "className" property must be set.'); } diff --git a/framework/validators/NumberValidator.php b/framework/validators/NumberValidator.php index 6219bdb..4d7297f 100644 --- a/framework/validators/NumberValidator.php +++ b/framework/validators/NumberValidator.php @@ -61,6 +61,10 @@ class NumberValidator extends Validator public function validateAttribute($object, $attribute) { $value = $object->$attribute; + if (is_array($value)) { + $this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.')); + return; + } if ($this->integerOnly) { if (!preg_match($this->integerPattern, "$value")) { $message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be an integer.'); diff --git a/framework/validators/RegularExpressionValidator.php b/framework/validators/RegularExpressionValidator.php index b0811e9..d88f613 100644 --- a/framework/validators/RegularExpressionValidator.php +++ b/framework/validators/RegularExpressionValidator.php @@ -51,8 +51,8 @@ class RegularExpressionValidator extends Validator public function validateAttribute($object, $attribute) { $value = $object->$attribute; - 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.'); + if (!$this->validateValue($value)) { + $message = $this->message !== null ? $this->message : \Yii::t('yii|{attribute} is invalid.'); $this->addError($object, $attribute, $message); } } @@ -64,8 +64,9 @@ class RegularExpressionValidator extends Validator */ public function validateValue($value) { - return !$this->not && preg_match($this->pattern, $value) - || $this->not && !preg_match($this->pattern, $value); + return !is_array($value) && + (!$this->not && preg_match($this->pattern, $value) + || $this->not && !preg_match($this->pattern, $value)); } /** diff --git a/framework/validators/UniqueValidator.php b/framework/validators/UniqueValidator.php index 8d5c8b7..30735b1 100644 --- a/framework/validators/UniqueValidator.php +++ b/framework/validators/UniqueValidator.php @@ -40,6 +40,12 @@ class UniqueValidator extends Validator public function validateAttribute($object, $attribute) { $value = $object->$attribute; + + if (is_array($value)) { + $this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.')); + return; + } + /** @var $className \yii\db\ActiveRecord */ $className = $this->className === null ? get_class($object) : \Yii::import($this->className); $attributeName = $this->attributeName === null ? $attribute : $this->attributeName; diff --git a/framework/validators/Validator.php b/framework/validators/Validator.php index 4dc58ae..b75f86e 100644 --- a/framework/validators/Validator.php +++ b/framework/validators/Validator.php @@ -192,7 +192,7 @@ abstract class Validator extends Component */ public function validateValue($value) { - throw new NotSupportedException(__CLASS__ . ' does not support validateValue().'); + throw new NotSupportedException(get_class($this) . ' does not support validateValue().'); } /**