Browse Source

finished validator refactoring.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
421e31ec0f
  1. 2
      framework/validators/CaptchaValidator.php
  2. 8
      framework/validators/ExistValidator.php
  3. 4
      framework/validators/NumberValidator.php
  4. 9
      framework/validators/RegularExpressionValidator.php
  5. 6
      framework/validators/UniqueValidator.php
  6. 2
      framework/validators/Validator.php

2
framework/validators/CaptchaValidator.php

@ -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);
} }
/** /**

8
framework/validators/ExistValidator.php

@ -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
*/ */
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.');
} }

4
framework/validators/NumberValidator.php

@ -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.');

9
framework/validators/RegularExpressionValidator.php

@ -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
*/ */
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));
} }
/** /**

6
framework/validators/UniqueValidator.php

@ -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;

2
framework/validators/Validator.php

@ -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().');
} }
/** /**

Loading…
Cancel
Save