Commit 421e31ec by Qiang Xue

finished validator refactoring.

parent 3bd186de
...@@ -53,7 +53,7 @@ class CaptchaValidator extends Validator ...@@ -53,7 +53,7 @@ class CaptchaValidator extends Validator
public function validateValue($value) public function validateValue($value)
{ {
$captcha = $this->getCaptchaAction(); $captcha = $this->getCaptchaAction();
return $captcha->validate($value, $this->caseSensitive); return !is_array($value) && $captcha->validate($value, $this->caseSensitive);
} }
/** /**
......
...@@ -48,6 +48,11 @@ class ExistValidator extends Validator ...@@ -48,6 +48,11 @@ class ExistValidator extends Validator
{ {
$value = $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 */ /** @var $className \yii\db\ActiveRecord */
$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;
...@@ -67,6 +72,9 @@ class ExistValidator extends Validator ...@@ -67,6 +72,9 @@ class ExistValidator extends Validator
*/ */
public function validateValue($value) public function validateValue($value)
{ {
if (is_array($value)) {
return false;
}
if ($this->className === null) { if ($this->className === null) {
throw new InvalidConfigException('The "className" property must be set.'); throw new InvalidConfigException('The "className" property must be set.');
} }
......
...@@ -61,6 +61,10 @@ class NumberValidator extends Validator ...@@ -61,6 +61,10 @@ class NumberValidator extends Validator
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if (is_array($value)) {
$this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.'));
return;
}
if ($this->integerOnly) { if ($this->integerOnly) {
if (!preg_match($this->integerPattern, "$value")) { if (!preg_match($this->integerPattern, "$value")) {
$message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be an integer.'); $message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be an integer.');
......
...@@ -51,8 +51,8 @@ class RegularExpressionValidator extends Validator ...@@ -51,8 +51,8 @@ class RegularExpressionValidator extends Validator
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ((!$this->not && !preg_match($this->pattern, $value)) || ($this->not && preg_match($this->pattern, $value))) { if (!$this->validateValue($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);
} }
} }
...@@ -64,8 +64,9 @@ class RegularExpressionValidator extends Validator ...@@ -64,8 +64,9 @@ class RegularExpressionValidator extends Validator
*/ */
public function validateValue($value) public function validateValue($value)
{ {
return !$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)
|| $this->not && !preg_match($this->pattern, $value));
} }
/** /**
......
...@@ -40,6 +40,12 @@ class UniqueValidator extends Validator ...@@ -40,6 +40,12 @@ class UniqueValidator extends Validator
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $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 */ /** @var $className \yii\db\ActiveRecord */
$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;
......
...@@ -192,7 +192,7 @@ abstract class Validator extends Component ...@@ -192,7 +192,7 @@ abstract class Validator extends Component
*/ */
public function validateValue($value) public function validateValue($value)
{ {
throw new NotSupportedException(__CLASS__ . ' does not support validateValue().'); throw new NotSupportedException(get_class($this) . ' does not support validateValue().');
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment