Permalink
Browse files

Refactored validators.

  • Loading branch information...
qiangxue committed Dec 6, 2013
1 parent 1c76175 commit cb4a8c764c9f8332dbfbf0ef3afd9c8687d92e46
Showing with 501 additions and 665 deletions.
  1. +2 −2 docs/guide/validation.md
  2. +1 −1 framework/yii/base/Model.php
  3. +6 −26 framework/yii/captcha/CaptchaValidator.php
  4. +11 −27 framework/yii/validators/BooleanValidator.php
  5. +35 −40 framework/yii/validators/CompareValidator.php
  6. +14 −19 framework/yii/validators/DateValidator.php
  7. +1 −3 framework/yii/validators/DefaultValueValidator.php
  8. +17 −38 framework/yii/validators/EmailValidator.php
  9. +6 −13 framework/yii/validators/ExistValidator.php
  10. +25 −32 framework/yii/validators/FileValidator.php
  11. +2 −7 framework/yii/validators/FilterValidator.php
  12. +17 −24 framework/yii/validators/ImageValidator.php
  13. +2 −22 framework/yii/validators/InlineValidator.php
  14. +18 −18 framework/yii/validators/NumberValidator.php
  15. +6 −27 framework/yii/validators/RangeValidator.php
  16. +6 −27 framework/yii/validators/RegularExpressionValidator.php
  17. +13 −33 framework/yii/validators/RequiredValidator.php
  18. +0 −6 framework/yii/validators/SafeValidator.php
  19. +19 −19 framework/yii/validators/StringValidator.php
  20. +2 −6 framework/yii/validators/UniqueValidator.php
  21. +12 −24 framework/yii/validators/UrlValidator.php
  22. +47 −12 framework/yii/validators/Validator.php
  23. +14 −14 tests/unit/framework/validators/BooleanValidatorTest.php
  24. +6 −6 tests/unit/framework/validators/CompareValidatorTest.php
  25. +9 −9 tests/unit/framework/validators/DateValidatorTest.php
  26. +47 −47 tests/unit/framework/validators/EmailValidatorTest.php
  27. +7 −7 tests/unit/framework/validators/ExistValidatorTest.php
  28. +54 −54 tests/unit/framework/validators/NumberValidatorTest.php
  29. +21 −21 tests/unit/framework/validators/RangeValidatorTest.php
  30. +8 −8 tests/unit/framework/validators/RegularExpressionValidatorTest.php
  31. +15 −15 tests/unit/framework/validators/RequiredValidatorTest.php
  32. +26 −26 tests/unit/framework/validators/StringValidatorTest.php
  33. +20 −20 tests/unit/framework/validators/UrlValidatorTest.php
  34. +12 −12 tests/unit/framework/validators/ValidatorTest.php
View
@@ -172,10 +172,10 @@ operate without model do. In our case to validate an email we can do the followi
```php
$email = 'test@example.com';
$validator = new yii\validators\EmailValidator();
-if ($validator->validateValue($email)) {
+if ($validator->validate($email, $error)) {
echo 'Email is valid.';
} else {
- echo 'Email is not valid.'
+ echo $error;
}
```
@@ -321,7 +321,7 @@ public function validate($attributes = null, $clearErrors = true)
}
if ($this->beforeValidate()) {
foreach ($this->getActiveValidators() as $validator) {
- $validator->validate($this, $attributes);
+ $validator->validateAttributes($this, $attributes);
}
$this->afterValidate();
return !$this->hasErrors();
@@ -38,7 +38,7 @@ class CaptchaValidator extends Validator
/**
- * Initializes the validator.
+ * @inheritdoc
*/
public function init()
{
@@ -49,28 +49,13 @@ public function init()
}
/**
- * 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 string $attribute the attribute being validated
+ * @inheritdoc
*/
- public function validateAttribute($object, $attribute)
- {
- $value = $object->$attribute;
- if (!$this->validateValue($value)) {
- $this->addError($object, $attribute, $this->message);
- }
- }
-
- /**
- * Validates the given value.
- * @param mixed $value the value to be validated.
- * @return boolean whether the value is valid.
- */
- public function validateValue($value)
+ protected function validateValue($value)
{
$captcha = $this->createCaptchaAction();
- return !is_array($value) && $captcha->validate($value, $this->caseSensitive);
+ $valid = !is_array($value) && $captcha->validate($value, $this->caseSensitive);
+ return $valid ? null : [$this->message, []];
}
/**
@@ -93,12 +78,7 @@ public function createCaptchaAction()
}
/**
- * 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.
- * @param \yii\web\View $view the view object that is going to be used to render views or view files
- * containing a model form with this validator applied.
- * @return string the client-side validation script.
+ * @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
{
@@ -37,7 +37,7 @@ class BooleanValidator extends Validator
public $strict = false;
/**
- * Initializes the validator.
+ * @inheritdoc
*/
public function init()
{
@@ -48,40 +48,24 @@ public function init()
}
/**
- * 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 string $attribute the attribute being validated
+ * @inheritdoc
*/
- public function validateAttribute($object, $attribute)
+ protected function validateValue($value)
{
- $value = $object->$attribute;
- if (!$this->validateValue($value)) {
- $this->addError($object, $attribute, $this->message, [
+ $valid = !$this->strict && ($value == $this->trueValue || $value == $this->falseValue)
+ || $this->strict && ($value === $this->trueValue || $value === $this->falseValue);
+ if (!$valid) {
+ return [$this->message, [
'true' => $this->trueValue,
'false' => $this->falseValue,
- ]);
+ ]];
+ } else {
+ return null;
}
}
/**
- * Validates the given value.
- * @param mixed $value the value to be validated.
- * @return boolean whether the value is valid.
- */
- public function validateValue($value)
- {
- return !$this->strict && ($value == $this->trueValue || $value == $this->falseValue)
- || $this->strict && ($value === $this->trueValue || $value === $this->falseValue);
- }
-
- /**
- * 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.
- * @param \yii\web\View $view the view object that is going to be used to render views or view files
- * containing a model form with this validator applied.
- * @return string the client-side validation script.
+ * @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
{
@@ -41,7 +41,7 @@ class CompareValidator extends Validator
*/
public $compareAttribute;
/**
- * @var string the constant value to be compared with. When both this property
+ * @var mixed the constant value to be compared with. When both this property
* and [[compareAttribute]] are set, this property takes precedence.
* @see compareAttribute
*/
@@ -66,12 +66,13 @@ class CompareValidator extends Validator
* - `{attribute}`: the label of the attribute being validated
* - `{value}`: the value of the attribute being validated
* - `{compareValue}`: the value or the attribute label to be compared with
+ * - `{compareAttribute}`: the label of the attribute to be compared with
*/
public $message;
/**
- * Initializes the validator.
+ * @inheritdoc
*/
public function init()
{
@@ -109,11 +110,7 @@ public function init()
}
/**
- * 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 string $attribute the attribute being validated
- * @throws InvalidConfigException if CompareValidator::operator is invalid
+ * @inheritdoc
*/
public function validateAttribute($object, $attribute)
{
@@ -130,18 +127,7 @@ public function validateAttribute($object, $attribute)
$compareLabel = $object->getAttributeLabel($compareAttribute);
}
- switch ($this->operator) {
- case '==': $valid = $value == $compareValue; break;
- case '===': $valid = $value === $compareValue; break;
- case '!=': $valid = $value != $compareValue; break;
- case '!==': $valid = $value !== $compareValue; break;
- case '>': $valid = $value > $compareValue; break;
- case '>=': $valid = $value >= $compareValue; break;
- case '<': $valid = $value < $compareValue; break;
- case '<=': $valid = $value <= $compareValue; break;
- default: $valid = false; break;
- }
- if (!$valid) {
+ if (!$this->compareValues($this->operator, $value, $compareValue)) {
$this->addError($object, $attribute, $this->message, [
'compareAttribute' => $compareLabel,
'compareValue' => $compareValue,
@@ -150,38 +136,47 @@ public function validateAttribute($object, $attribute)
}
/**
- * Validates the given value.
- * @param mixed $value the value to be validated.
- * @return boolean whether the value is valid.
- * @throws InvalidConfigException if [[compareValue]] is not set.
+ * @inheritdoc
*/
- public function validateValue($value)
+ protected function validateValue($value)
{
if ($this->compareValue === null) {
throw new InvalidConfigException('CompareValidator::compareValue must be set.');
}
+ if (!$this->compareValues($this->operator, $value, $this->compareValue)) {
+ return [$this->message, [
+ 'compareAttribute' => $this->compareValue,
+ 'compareValue' => $this->compareValue,
+ ]];
+ } else {
+ return null;
+ }
+ }
- switch ($this->operator) {
- case '==': return $value == $this->compareValue;
- case '===': return $value === $this->compareValue;
- case '!=': return $value != $this->compareValue;
- case '!==': return $value !== $this->compareValue;
- case '>': return $value > $this->compareValue;
- case '>=': return $value >= $this->compareValue;
- case '<': return $value < $this->compareValue;
- case '<=': return $value <= $this->compareValue;
+ /**
+ * Compares two values with the specified operator.
+ * @param string $operator the comparison operator
+ * @param mixed $value the value being compared
+ * @param mixed $compareValue another value being compared
+ * @return boolean whether the comparison using the specified operator is true.
+ */
+ protected function compareValues($operator, $value, $compareValue)
+ {
+ switch ($operator) {
+ case '==': return $value == $compareValue;
+ case '===': return $value === $compareValue;
+ case '!=': return $value != $compareValue;
+ case '!==': return $value !== $compareValue;
+ case '>': return $value > $compareValue;
+ case '>=': return $value >= $compareValue;
+ case '<': return $value < $compareValue;
+ case '<=': return $value <= $compareValue;
default: return false;
}
}
/**
- * 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 \yii\web\View $view the view object that is going to be used to render views or view files
- * containing a model form with this validator applied.
- * @throws InvalidConfigException if CompareValidator::operator is invalid
+ * @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
{
@@ -32,7 +32,7 @@ class DateValidator extends Validator
public $timestampAttribute;
/**
- * Initializes the validator.
+ * @inheritdoc
*/
public function init()
{
@@ -43,36 +43,31 @@ public function init()
}
/**
- * 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 string $attribute the attribute being validated
+ * @inheritdoc
*/
public function validateAttribute($object, $attribute)
{
$value = $object->$attribute;
- if (is_array($value)) {
- $this->addError($object, $attribute, $this->message);
- return;
- }
- $date = DateTime::createFromFormat($this->format, $value);
- $errors = DateTime::getLastErrors();
- if ($date === false || $errors['error_count'] || $errors['warning_count']) {
- $this->addError($object, $attribute, $this->message);
+ $result = $this->validateValue($value);
+ if (!empty($result)) {
+ $this->addError($object, $attribute, $result[0], $result[1]);
} elseif ($this->timestampAttribute !== null) {
+ $date = DateTime::createFromFormat($this->format, $value);
$object->{$this->timestampAttribute} = $date->getTimestamp();
}
}
/**
- * Validates the given value.
- * @param mixed $value the value to be validated.
- * @return boolean whether the value is valid.
+ * @inheritdoc
*/
- public function validateValue($value)
+ protected function validateValue($value)
{
- DateTime::createFromFormat($this->format, $value);
+ if (is_array($value)) {
+ return [$this->message, []];
+ }
+ $date = DateTime::createFromFormat($this->format, $value);
$errors = DateTime::getLastErrors();
- return $errors['error_count'] === 0 && $errors['warning_count'] === 0;
+ $invalid = $date === false || $errors['error_count'] || $errors['warning_count'];
+ return $invalid ? [$this->message, []] : null;
}
}
@@ -29,9 +29,7 @@ class DefaultValueValidator extends Validator
public $skipOnEmpty = false;
/**
- * Validates the attribute of the object.
- * @param \yii\base\Model $object the object being validated
- * @param string $attribute the attribute being validated
+ * @inheritdoc
*/
public function validateAttribute($object, $attribute)
{
Oops, something went wrong.

3 comments on commit cb4a8c7

@jom

This comment has been minimized.

Show comment
Hide comment
@jom

jom Dec 6, 2013

Contributor

SafeValidator no longer works. Should we be making attributes as safe with scenarios instead?

Contributor

jom replied Dec 6, 2013

SafeValidator no longer works. Should we be making attributes as safe with scenarios instead?

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Dec 7, 2013

Member

Nope, should be fixed. @qiangxue am I right that validateValue() has to be implemented to return null in SafeValidator?

Member

cebe replied Dec 7, 2013

Nope, should be fixed. @qiangxue am I right that validateValue() has to be implemented to return null in SafeValidator?

@egorpromo

This comment has been minimized.

Show comment
Hide comment
@egorpromo

egorpromo Dec 7, 2013

Contributor

@qiangxue Good work 👍

Contributor

egorpromo replied Dec 7, 2013

@qiangxue Good work 👍

Please sign in to comment.