From ae6c69fc8fa2ca15299fe5de6e3c1a9045ab42ad Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 5 Apr 2013 08:32:13 -0400 Subject: [PATCH] refacotring validators. --- framework/validators/BooleanValidator.php | 19 +++++++--- framework/validators/CaptchaValidator.php | 19 +++++++--- framework/validators/CompareValidator.php | 2 +- framework/validators/DateValidator.php | 18 ++++++++-- framework/validators/EmailValidator.php | 21 ++++++++--- framework/validators/ExistValidator.php | 17 +++++++-- framework/validators/NumberValidator.php | 2 +- framework/validators/RangeValidator.php | 17 ++++----- .../validators/RegularExpressionValidator.php | 12 ++++--- framework/validators/RequiredValidator.php | 30 +++++++++------- framework/validators/StringValidator.php | 41 +++++++++++----------- framework/validators/UniqueValidator.php | 18 ++++++++-- framework/validators/UrlValidator.php | 22 +++++++++--- 13 files changed, 161 insertions(+), 77 deletions(-) diff --git a/framework/validators/BooleanValidator.php b/framework/validators/BooleanValidator.php index b441108..6d2c671 100644 --- a/framework/validators/BooleanValidator.php +++ b/framework/validators/BooleanValidator.php @@ -36,6 +36,17 @@ class BooleanValidator extends Validator public $strict = false; /** + * Initializes the validator. + */ + public function init() + { + parent::init(); + if ($this->message === null) { + $this->message = Yii::t('yii|{attribute} must be either "{true}" or "{false}".'); + } + } + + /** * 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 @@ -45,8 +56,7 @@ class BooleanValidator extends Validator { $value = $object->$attribute; if (!$this->validateValue($value)) { - $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, $this->message, array( '{true}' => $this->trueValue, '{false}' => $this->falseValue, )); @@ -72,15 +82,14 @@ class BooleanValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be either "{true}" or "{false}".'); - $message = strtr($message, array( + $message = strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, '{true}' => $this->trueValue, '{false}' => $this->falseValue, )); return " -if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . "value!=" . json_encode($this->trueValue) . " && value!=" . json_encode($this->falseValue) . ") { +if(" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . "value!=" . json_encode($this->trueValue) . " && value!=" . json_encode($this->falseValue) . ") { messages.push(" . json_encode($message) . "); } "; diff --git a/framework/validators/CaptchaValidator.php b/framework/validators/CaptchaValidator.php index 65e7fd3..3b4745b 100644 --- a/framework/validators/CaptchaValidator.php +++ b/framework/validators/CaptchaValidator.php @@ -31,6 +31,17 @@ class CaptchaValidator extends Validator /** + * Initializes the validator. + */ + public function init() + { + parent::init(); + if ($this->message === null) { + $this->message = Yii::t('yii|The verification code is incorrect.'); + } + } + + /** * 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 @@ -40,8 +51,7 @@ class CaptchaValidator extends Validator { $value = $object->$attribute; if (!$this->validateValue($value)) { - $message = $this->message !== null ? $this->message : Yii::t('yii|The verification code is incorrect.'); - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } } @@ -83,8 +93,7 @@ class CaptchaValidator extends Validator public function clientValidateAttribute($object, $attribute) { $captcha = $this->getCaptchaAction(); - $message = $this->message !== null ? $this->message : \Yii::t('yii|The verification code is incorrect.'); - $message = strtr($message, array( + $message = strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, )); @@ -102,7 +111,7 @@ if(h != hash) { } "; - if ($this->allowEmpty) { + if ($this->skipOnEmpty) { $js = " if($.trim(value)!='') { $js diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index 3c85367..eb3edc6 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -223,7 +223,7 @@ class CompareValidator extends Validator )); return " -if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { +if (" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { messages.push(" . json_encode($message) . "); } "; diff --git a/framework/validators/DateValidator.php b/framework/validators/DateValidator.php index 7d0793d..7c3b181 100644 --- a/framework/validators/DateValidator.php +++ b/framework/validators/DateValidator.php @@ -32,6 +32,17 @@ class DateValidator extends Validator public $timestampAttribute; /** + * Initializes the validator. + */ + public function init() + { + parent::init(); + if ($this->message === null) { + $this->message = Yii::t('yii|The format of {attribute} is invalid.'); + } + } + + /** * 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 @@ -40,10 +51,13 @@ class DateValidator extends Validator 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); if ($date === false) { - $message = $this->message !== null ? $this->message : Yii::t('yii|The format of {attribute} is invalid.'); - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } elseif ($this->timestampAttribute !== false) { $object->{$this->timestampAttribute} = $date->getTimestamp(); } diff --git a/framework/validators/EmailValidator.php b/framework/validators/EmailValidator.php index 396c25f..e498975 100644 --- a/framework/validators/EmailValidator.php +++ b/framework/validators/EmailValidator.php @@ -7,6 +7,8 @@ namespace yii\validators; +use Yii; + /** * EmailValidator validates that the attribute value is a valid email address. * @@ -44,6 +46,17 @@ class EmailValidator extends Validator public $checkPort = false; /** + * Initializes the validator. + */ + public function init() + { + parent::init(); + if ($this->message === null) { + $this->message = Yii::t('yii|{attribute} is not a valid email address.'); + } + } + + /** * 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 @@ -53,8 +66,7 @@ class EmailValidator extends Validator { $value = $object->$attribute; if (!$this->validateValue($value)) { - $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, $this->message); } } @@ -88,8 +100,7 @@ class EmailValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid email address.'); - $message = strtr($message, array( + $message = strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, )); @@ -100,7 +111,7 @@ class EmailValidator extends Validator } return " -if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { +if(" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { messages.push(" . json_encode($message) . "); } "; diff --git a/framework/validators/ExistValidator.php b/framework/validators/ExistValidator.php index ec01134..7aa434c 100644 --- a/framework/validators/ExistValidator.php +++ b/framework/validators/ExistValidator.php @@ -37,6 +37,18 @@ class ExistValidator extends Validator */ public $attributeName; + + /** + * Initializes the validator. + */ + public function init() + { + parent::init(); + if ($this->message === null) { + $this->message = Yii::t('yii|{attribute} is invalid.'); + } + } + /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. @@ -49,7 +61,7 @@ class ExistValidator extends Validator $value = $object->$attribute; if (is_array($value)) { - $this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.')); + $this->addError($object, $attribute, $this->message); return; } @@ -59,8 +71,7 @@ class ExistValidator extends Validator $query = $className::find(); $query->where(array($attributeName => $value)); if (!$query->exists()) { - $message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} "{value}" is invalid.'); - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } } diff --git a/framework/validators/NumberValidator.php b/framework/validators/NumberValidator.php index 4d7297f..226df12 100644 --- a/framework/validators/NumberValidator.php +++ b/framework/validators/NumberValidator.php @@ -152,7 +152,7 @@ if(value>{$this->max}) { "; } - if ($this->allowEmpty) { + if ($this->skipOnEmpty) { $js = " if(jQuery.trim(value)!='') { $js diff --git a/framework/validators/RangeValidator.php b/framework/validators/RangeValidator.php index 0498a55..18742ae 100644 --- a/framework/validators/RangeValidator.php +++ b/framework/validators/RangeValidator.php @@ -6,6 +6,8 @@ */ namespace yii\validators; + +use Yii; use yii\base\InvalidConfigException; /** @@ -44,6 +46,9 @@ class RangeValidator extends Validator if (!is_array($this->range)) { throw new InvalidConfigException('The "range" property must be set.'); } + if ($this->message === null) { + $this->message = Yii::t('yii|{attribute} is invalid.'); + } } /** @@ -55,11 +60,10 @@ class RangeValidator extends Validator public function validateAttribute($object, $attribute) { $value = $object->$attribute; - $message = $this->message !== null ? $this->message : \Yii::t('yii|{attribute} is invalid.'); if (!$this->not && !in_array($value, $this->range, $this->strict)) { - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } elseif ($this->not && in_array($value, $this->range, $this->strict)) { - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } } @@ -82,10 +86,7 @@ class RangeValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - if (($message = $this->message) === null) { - $message = \Yii::t('yii|{attribute} is invalid.'); - } - $message = strtr($message, array( + $message = strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, )); @@ -97,7 +98,7 @@ class RangeValidator extends Validator $range = json_encode($range); return " -if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? "$.inArray(value, $range)>=0" : "$.inArray(value, $range)<0") . ") { +if (" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? "$.inArray(value, $range)>=0" : "$.inArray(value, $range)<0") . ") { messages.push(" . json_encode($message) . "); } "; diff --git a/framework/validators/RegularExpressionValidator.php b/framework/validators/RegularExpressionValidator.php index d88f613..6c69be3 100644 --- a/framework/validators/RegularExpressionValidator.php +++ b/framework/validators/RegularExpressionValidator.php @@ -7,6 +7,7 @@ namespace yii\validators; +use Yii; use yii\base\InvalidConfigException; /** @@ -40,6 +41,9 @@ class RegularExpressionValidator extends Validator if ($this->pattern === null) { throw new InvalidConfigException('The "pattern" property must be set.'); } + if ($this->message === null) { + $this->message = Yii::t('yii|{attribute} is invalid.'); + } } /** @@ -52,8 +56,7 @@ class RegularExpressionValidator extends Validator { $value = $object->$attribute; if (!$this->validateValue($value)) { - $message = $this->message !== null ? $this->message : \Yii::t('yii|{attribute} is invalid.'); - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } } @@ -78,8 +81,7 @@ class RegularExpressionValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is invalid.'); - $message = strtr($message, array( + $message = strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, )); @@ -99,7 +101,7 @@ class RegularExpressionValidator extends Validator } return " -if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { +if (" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { messages.push(" . json_encode($message) . "); } "; diff --git a/framework/validators/RequiredValidator.php b/framework/validators/RequiredValidator.php index 944c695..febee9b 100644 --- a/framework/validators/RequiredValidator.php +++ b/framework/validators/RequiredValidator.php @@ -7,6 +7,8 @@ namespace yii\validators; +use Yii; + /** * RequiredValidator validates that the specified attribute does not have null or empty value. * @@ -39,6 +41,17 @@ class RequiredValidator extends Validator public $strict = false; /** + * Initializes the validator. + */ + public function init() + { + parent::init(); + if ($this->message === null) { + $this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} is invalid.') : Yii::t('yii|{attribute} must be "{requiredValue}".'); + } + } + + /** * 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 @@ -49,13 +62,11 @@ class RequiredValidator extends Validator $value = $object->$attribute; if ($this->requiredValue === null) { if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) { - $message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} cannot be blank.'); - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } } else { if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) { - $message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} must be "{requiredValue}".'); - $this->addError($object, $attribute, $message, array( + $this->addError($object, $attribute, $this->message, array( '{requiredValue}' => $this->requiredValue, )); } @@ -87,12 +98,8 @@ class RequiredValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = $this->message; if ($this->requiredValue !== null) { - if ($message === null) { - $message = \Yii::t('yii|{attribute} must be "{requiredValue}".'); - } - $message = strtr($message, array( + $message = strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, '{requiredValue}' => $this->requiredValue, @@ -103,10 +110,7 @@ if (value != " . json_encode($this->requiredValue) . ") { } "; } else { - if ($message === null) { - $message = \Yii::t('yii|{attribute} cannot be blank.'); - } - $message = strtr($message, array( + $message = strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, )); diff --git a/framework/validators/StringValidator.php b/framework/validators/StringValidator.php index 83ff35b..8b8c73b 100644 --- a/framework/validators/StringValidator.php +++ b/framework/validators/StringValidator.php @@ -63,6 +63,18 @@ class StringValidator extends Validator if ($this->encoding === null) { $this->encoding = Yii::$app->charset; } + if ($this->message === null) { + $this->message = Yii::t('yii|{attribute} must be a string.'); + } + if ($this->min !== null && $this->tooShort === null) { + $this->tooShort = Yii::t('yii|{attribute} should contain at least {min} characters.'); + } + if ($this->max !== null && $this->tooLong === null) { + $this->tooLong = Yii::t('yii|{attribute} should contain at most {max} characters.'); + } + if ($this->is !== null && $this->notEqual === null) { + $this->notEqual = Yii::t('yii|{attribute} should contain {length} characters.'); + } } /** @@ -76,24 +88,20 @@ class StringValidator extends Validator $value = $object->$attribute; if (!is_string($value)) { - $message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be a string.'); - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); return; } $length = mb_strlen($value, $this->encoding); if ($this->min !== null && $length < $this->min) { - $message = ($this->tooShort !== null) ? $this->tooShort : Yii::t('yii|{attribute} should contain at least {min} characters.'); - $this->addError($object, $attribute, $message, array('{min}' => $this->min)); + $this->addError($object, $attribute, $this->tooShort, array('{min}' => $this->min)); } if ($this->max !== null && $length > $this->max) { - $message = ($this->tooLong !== null) ? $this->tooLong : Yii::t('yii|{attribute} should contain at most {max} characters.'); - $this->addError($object, $attribute, $message, array('{max}' => $this->max)); + $this->addError($object, $attribute, $this->tooLong, array('{max}' => $this->max)); } if ($this->is !== null && $length !== $this->is) { - $message = ($this->notEqual !== null) ? $this->notEqual : Yii::t('yii|{attribute} should contain {length} characters.'); - $this->addError($object, $attribute, $message, array('{length}' => $this->is)); + $this->addError($object, $attribute, $this->notEqual, array('{length}' => $this->is)); } } @@ -124,28 +132,19 @@ class StringValidator extends Validator $label = $object->getAttributeLabel($attribute); $value = $object->$attribute; - if (($notEqual = $this->notEqual) === null) { - $notEqual = Yii::t('yii|{attribute} should contain {length} characters.'); - } - $notEqual = strtr($notEqual, array( + $notEqual = strtr($this->notEqual, array( '{attribute}' => $label, '{value}' => $value, '{length}' => $this->is, )); - if (($tooShort = $this->tooShort) === null) { - $tooShort = Yii::t('yii|{attribute} should contain at least {min} characters.'); - } - $tooShort = strtr($tooShort, array( + $tooShort = strtr($this->tooShort, array( '{attribute}' => $label, '{value}' => $value, '{min}' => $this->min, )); - if (($tooLong = $this->tooLong) === null) { - $tooLong = Yii::t('yii|{attribute} should contain at most {max} characters.'); - } - $tooLong = strtr($tooLong, array( + $tooLong = strtr($this->tooLong, array( '{attribute}' => $label, '{value}' => $value, '{max}' => $this->max, @@ -174,7 +173,7 @@ if(value.length!= {$this->is}) { "; } - if ($this->allowEmpty) { + if ($this->skipOnEmpty) { $js = " if($.trim(value)!='') { $js diff --git a/framework/validators/UniqueValidator.php b/framework/validators/UniqueValidator.php index 30735b1..fa55df7 100644 --- a/framework/validators/UniqueValidator.php +++ b/framework/validators/UniqueValidator.php @@ -6,6 +6,8 @@ */ namespace yii\validators; + +use Yii; use yii\base\InvalidConfigException; /** @@ -31,6 +33,17 @@ class UniqueValidator extends Validator public $attributeName; /** + * Initializes the validator. + */ + public function init() + { + parent::init(); + if ($this->message === null) { + $this->message = Yii::t('yii|{attribute} "{value}" has already been taken.'); + } + } + + /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. * @param \yii\db\ActiveRecord $object the object being validated @@ -52,7 +65,7 @@ class UniqueValidator extends Validator $table = $className::getTableSchema(); if (($column = $table->getColumn($attributeName)) === null) { - throw new InvalidConfigException('Table "' . $table->name . '" does not have a column named "' . $attributeName . '"'); + throw new InvalidConfigException("Table '{$table->name}' does not have a column named '$attributeName'."); } $query = $className::find(); @@ -81,8 +94,7 @@ class UniqueValidator extends Validator } if ($exists) { - $message = $this->message !== null ? $this->message : \Yii::t('yii|{attribute} "{value}" has already been taken.'); - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } } } \ No newline at end of file diff --git a/framework/validators/UrlValidator.php b/framework/validators/UrlValidator.php index fb743e0..cd6bfef 100644 --- a/framework/validators/UrlValidator.php +++ b/framework/validators/UrlValidator.php @@ -7,6 +7,8 @@ namespace yii\validators; +use Yii; + /** * UrlValidator validates that the attribute value is a valid http or https URL. * @@ -33,6 +35,18 @@ class UrlValidator extends Validator **/ public $defaultScheme; + + /** + * Initializes the validator. + */ + public function init() + { + parent::init(); + if ($this->message === null) { + $this->message = Yii::t('yii|{attribute} is not a valid URL.'); + } + } + /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. @@ -47,8 +61,7 @@ class UrlValidator extends Validator $object->$attribute = $this->defaultScheme . '://' . $value; } } else { - $message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid URL.'); - $this->addError($object, $attribute, $message); + $this->addError($object, $attribute, $this->message); } } @@ -87,8 +100,7 @@ class UrlValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid URL.'); - $message = strtr($message, array( + $message = strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, )); @@ -113,7 +125,7 @@ $js "; } - if ($this->allowEmpty) { + if ($this->skipOnEmpty) { $js = " if($.trim(value)!='') { $js