From ef4ed7c80324af2d14f1e3b239612bd4341a80fc Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 22 Jul 2011 16:13:07 -0400 Subject: [PATCH] w --- framework/validators/BooleanValidator.php | 8 +-- framework/validators/CaptchaValidator.php | 8 +-- framework/validators/CompareValidator.php | 8 +-- framework/validators/DateValidator.php | 4 +- framework/validators/DefaultValueValidator.php | 4 +- framework/validators/EmailValidator.php | 6 +- framework/validators/ExistValidator.php | 4 +- framework/validators/FileValidator.php | 10 +-- framework/validators/FilterValidator.php | 4 +- framework/validators/InlineValidator.php | 24 ++++--- framework/validators/NumberValidator.php | 10 +-- framework/validators/RangeValidator.php | 8 +-- .../validators/RegularExpressionValidator.php | 6 +- framework/validators/RequiredValidator.php | 55 +++++++++++----- framework/validators/SafeValidator.php | 4 +- framework/validators/StringValidator.php | 10 +-- framework/validators/TypeValidator.php | 4 +- framework/validators/UniqueValidator.php | 4 +- framework/validators/UnsafeValidator.php | 4 +- framework/validators/UrlValidator.php | 8 +-- framework/validators/Validator.php | 76 +++++++++++----------- 21 files changed, 147 insertions(+), 122 deletions(-) diff --git a/framework/validators/BooleanValidator.php b/framework/validators/BooleanValidator.php index 1203073..040553b 100644 --- a/framework/validators/BooleanValidator.php +++ b/framework/validators/BooleanValidator.php @@ -17,7 +17,7 @@ namespace yii\validators; * @package system.validators * @since 1.0.10 */ -class CBooleanValidator extends CValidator +class CBooleanValidator extends Validator { /** * @var mixed the value representing true status. Defaults to '1'. @@ -45,7 +45,7 @@ class CBooleanValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -78,8 +78,8 @@ class CBooleanValidator extends CValidator '{false}' => $this->falseValue, )); return " -if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . "value!=" . CJSON::encode($this->trueValue) . " && value!=" . CJSON::encode($this->falseValue) . ") { - messages.push(" . CJSON::encode($message) . "); +if(" . ($this->allowEmpty ? "$.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 eca3fe1..101510b 100644 --- a/framework/validators/CaptchaValidator.php +++ b/framework/validators/CaptchaValidator.php @@ -19,7 +19,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CCaptchaValidator extends CValidator +class CCaptchaValidator extends Validator { /** * @var boolean whether the comparison is case sensitive. Defaults to false. @@ -43,7 +43,7 @@ class CCaptchaValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -61,7 +61,7 @@ class CCaptchaValidator extends CValidator * @return CCaptchaAction the action object * @since 1.1.7 */ - protected function getCaptchaAction() + public function getCaptchaAction() { if (($captcha = Yii::app()->getController()->createAction($this->captchaAction)) === null) { @@ -105,7 +105,7 @@ else hash = hash[" . ($this->caseSensitive ? 0 : 1) . "]; for(var i=value.length-1, h=0; i >= 0; --i) h+=value." . ($this->caseSensitive ? '' : 'toLowerCase().') . "charCodeAt(i); if(h != hash) { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index 374bf5c..6a02d9f 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -29,7 +29,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CCompareValidator extends CValidator +class CCompareValidator extends Validator { /** * @var string the name of the attribute to be compared with @@ -72,7 +72,7 @@ class CCompareValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -149,7 +149,7 @@ class CCompareValidator extends CValidator if ($this->compareValue !== null) { $compareTo = $this->compareValue; - $compareValue = CJSON::encode($this->compareValue); + $compareValue = json_encode($this->compareValue); } else { @@ -203,7 +203,7 @@ class CCompareValidator extends CValidator return " if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; } diff --git a/framework/validators/DateValidator.php b/framework/validators/DateValidator.php index a4d266f..f7f079d 100644 --- a/framework/validators/DateValidator.php +++ b/framework/validators/DateValidator.php @@ -20,7 +20,7 @@ namespace yii\validators; * @package system.validators * @since 1.1.7 */ -class CDateValidator extends CValidator +class CDateValidator extends Validator { /** * @var mixed the format pattern that the date value should follow. @@ -47,7 +47,7 @@ class CDateValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) diff --git a/framework/validators/DefaultValueValidator.php b/framework/validators/DefaultValueValidator.php index 7ad68f2..1f00465 100644 --- a/framework/validators/DefaultValueValidator.php +++ b/framework/validators/DefaultValueValidator.php @@ -19,7 +19,7 @@ namespace yii\validators; * @package system.validators * @since 1.0.2 */ -class CDefaultValueValidator extends CValidator +class CDefaultValueValidator extends Validator { /** * @var mixed the default value to be set to the specified attributes. @@ -37,7 +37,7 @@ class CDefaultValueValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { if (!$this->setOnEmpty) $object->$attribute = $this->value; diff --git a/framework/validators/EmailValidator.php b/framework/validators/EmailValidator.php index b2c4c24..41a5f55 100644 --- a/framework/validators/EmailValidator.php +++ b/framework/validators/EmailValidator.php @@ -17,7 +17,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CEmailValidator extends CValidator +class CEmailValidator extends Validator { /** * @var string the regular expression used to validate the attribute value. @@ -61,7 +61,7 @@ class CEmailValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -115,7 +115,7 @@ class CEmailValidator extends CValidator return " if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; } diff --git a/framework/validators/ExistValidator.php b/framework/validators/ExistValidator.php index d82972c..e82a911 100644 --- a/framework/validators/ExistValidator.php +++ b/framework/validators/ExistValidator.php @@ -20,7 +20,7 @@ namespace yii\validators; * @package system.validators * @since 1.0.4 */ -class CExistValidator extends CValidator +class CExistValidator extends Validator { /** * @var string the ActiveRecord class name that should be used to @@ -56,7 +56,7 @@ class CExistValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) diff --git a/framework/validators/FileValidator.php b/framework/validators/FileValidator.php index 5e603e0..0b1a3fb 100644 --- a/framework/validators/FileValidator.php +++ b/framework/validators/FileValidator.php @@ -44,7 +44,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CFileValidator extends CValidator +class CFileValidator extends Validator { /** * @var boolean whether the attribute requires a file to be uploaded or not. @@ -106,7 +106,7 @@ class CFileValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { if ($this->maxFiles > 1) { @@ -143,7 +143,7 @@ class CFileValidator extends CValidator * @param string $attribute the attribute being validated * @param CUploadedFile $file uploaded file passed to check against a set of rules */ - protected function validateFile($object, $attribute, $file) + public function validateFile($object, $attribute, $file) { if (null === $file || ($error = $file->getError()) == UPLOAD_ERR_NO_FILE) return $this->emptyAttribute($object, $attribute); @@ -186,7 +186,7 @@ class CFileValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function emptyAttribute($object, $attribute) + public function emptyAttribute($object, $attribute) { if (!$this->allowEmpty) { @@ -206,7 +206,7 @@ class CFileValidator extends CValidator * * @return integer the size limit for uploaded files. */ - protected function getSizeLimit() + public function getSizeLimit() { $limit = ini_get('upload_max_filesize'); $limit = $this->sizeToBytes($limit); diff --git a/framework/validators/FilterValidator.php b/framework/validators/FilterValidator.php index bc7ea83..ebffe85 100644 --- a/framework/validators/FilterValidator.php +++ b/framework/validators/FilterValidator.php @@ -28,7 +28,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CFilterValidator extends CValidator +class CFilterValidator extends Validator { /** * @var callback the filter method @@ -41,7 +41,7 @@ class CFilterValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { if ($this->filter === null || !is_callable($this->filter)) throw new CException(Yii::t('yii', 'The "filter" property must be specified with a valid callback.')); diff --git a/framework/validators/InlineValidator.php b/framework/validators/InlineValidator.php index 099f0e4..dd5d5be 100644 --- a/framework/validators/InlineValidator.php +++ b/framework/validators/InlineValidator.php @@ -1,6 +1,6 @@ - * @version $Id: CInlineValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $ - * @package system.validators - * @since 1.0 + * @since 2.0 */ -class CInlineValidator extends CValidator +class InlineValidator extends Validator { /** * @var string the name of the validation method defined in the active record class @@ -30,11 +37,10 @@ class CInlineValidator extends CValidator /** * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param CModel $object the object being validated + * @param \yii\base\Model $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $method = $this->method; $object->$method($attribute, $this->params); diff --git a/framework/validators/NumberValidator.php b/framework/validators/NumberValidator.php index 8d20582..bc19287 100644 --- a/framework/validators/NumberValidator.php +++ b/framework/validators/NumberValidator.php @@ -17,7 +17,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CNumberValidator extends CValidator +class CNumberValidator extends Validator { /** * @var boolean whether the attribute value can only be an integer. Defaults to false. @@ -62,7 +62,7 @@ class CNumberValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -130,14 +130,14 @@ class CNumberValidator extends CValidator $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; $js = " if(!value.match($pattern)) { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; if ($this->min !== null) { $js .= " if(value< {$this->min}) { - messages.push(" . CJSON::encode($tooSmall) . "); + messages.push(" . json_encode($tooSmall) . "); } "; } @@ -145,7 +145,7 @@ if(value< {$this->min}) { { $js .= " if(value> {$this->max}) { - messages.push(" . CJSON::encode($tooBig) . "); + messages.push(" . json_encode($tooBig) . "); } "; } diff --git a/framework/validators/RangeValidator.php b/framework/validators/RangeValidator.php index bfdec57..4a6ec1e 100644 --- a/framework/validators/RangeValidator.php +++ b/framework/validators/RangeValidator.php @@ -18,7 +18,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CRangeValidator extends CValidator +class CRangeValidator extends Validator { /** * @var array list of valid values that the attribute value should be among @@ -46,7 +46,7 @@ class CRangeValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -87,11 +87,11 @@ class CRangeValidator extends CValidator $range = array(); foreach ($this->range as $value) $range[] = (string)$value; - $range = CJSON::encode($range); + $range = json_encode($range); return " if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? "$.inArray(value, $range)>=0" : "$.inArray(value, $range)<0") . ") { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; } diff --git a/framework/validators/RegularExpressionValidator.php b/framework/validators/RegularExpressionValidator.php index cda8ecb..dea4a8b 100644 --- a/framework/validators/RegularExpressionValidator.php +++ b/framework/validators/RegularExpressionValidator.php @@ -18,7 +18,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CRegularExpressionValidator extends CValidator +class CRegularExpressionValidator extends Validator { /** * @var string the regular expression to be matched with @@ -42,7 +42,7 @@ class CRegularExpressionValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -88,7 +88,7 @@ class CRegularExpressionValidator extends CValidator return " if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; } diff --git a/framework/validators/RequiredValidator.php b/framework/validators/RequiredValidator.php index 8281267..3ed1ade 100644 --- a/framework/validators/RequiredValidator.php +++ b/framework/validators/RequiredValidator.php @@ -1,6 +1,6 @@ - * @version $Id: CRequiredValidator.php 3157 2011-04-02 19:21:06Z qiang.xue $ - * @package system.validators - * @since 1.0 + * @since 2.0 */ -class CRequiredValidator extends CValidator +class RequiredValidator extends Validator { /** * @var mixed the desired value that the attribute must have. - * If this is null, the validator will validate that the specified attribute does not have null or empty value. + * If this is null, the validator will validate that the specified attribute is not empty. * If this is set as a value that is not null, the validator will validate that * the attribute has a value that is the same as this property value. * Defaults to null. - * @since 1.0.10 + * @see strict */ public $requiredValue; /** - * @var boolean whether the comparison to {@link requiredValue} is strict. - * When this is true, the attribute value and type must both match those of {@link requiredValue}. - * Defaults to false, meaning only the value needs to be matched. - * This property is only used when {@link requiredValue} is not null. - * @since 1.0.10 + * @var boolean whether the comparison between the attribute value and [[requiredValue]] is strict. + * When this is true, both the values and types must match. + * Defaults to false, meaning only the values need to match. + * Note that when [[requiredValue]] is null, if this property is true, the validator will check + * if the attribute value is null; If this property is false, the validator will call [[isEmpty]] + * to check if the attribute value is empty. */ public $strict = false; + + /** + * Validates a value. + * @param mixed $value the value being validated. + * @return boolean whether the value is valid. + */ + public function validateValue($value) + { + if ($this->requiredValue !== null) { + if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) + { + $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be {value}.', + array('{value}' => $this->requiredValue)); + $this->addError($object, $attribute, $message); + } + } + elseif ($this->isEmpty($value, true)) { + $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.'); + $this->addError($object, $attribute, $message); + } + } + /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->requiredValue !== null) @@ -81,8 +102,8 @@ class CRequiredValidator extends CValidator '{attribute}' => $object->getAttributeLabel($attribute), )); return " -if(value!=" . CJSON::encode($this->requiredValue) . ") { - messages.push(" . CJSON::encode($message) . "); +if(value!=" . json_encode($this->requiredValue) . ") { + messages.push(" . json_encode($message) . "); } "; } @@ -95,7 +116,7 @@ if(value!=" . CJSON::encode($this->requiredValue) . ") { )); return " if($.trim(value)=='') { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; } diff --git a/framework/validators/SafeValidator.php b/framework/validators/SafeValidator.php index e77718d..7119977 100644 --- a/framework/validators/SafeValidator.php +++ b/framework/validators/SafeValidator.php @@ -17,7 +17,7 @@ namespace yii\validators; * @package system.validators * @since 1.1 */ -class CSafeValidator extends CValidator +class CSafeValidator extends Validator { /** * Validates the attribute of the object. @@ -25,7 +25,7 @@ class CSafeValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { } } diff --git a/framework/validators/StringValidator.php b/framework/validators/StringValidator.php index dd2576a..33a5297 100644 --- a/framework/validators/StringValidator.php +++ b/framework/validators/StringValidator.php @@ -19,7 +19,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CStringValidator extends CValidator +class CStringValidator extends Validator { /** * @var integer maximum length. Defaults to null, meaning no maximum limit. @@ -63,7 +63,7 @@ class CStringValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -129,7 +129,7 @@ class CStringValidator extends CValidator { $js .= " if(value.length< {$this->min}) { - messages.push(" . CJSON::encode($tooShort) . "); + messages.push(" . json_encode($tooShort) . "); } "; } @@ -137,7 +137,7 @@ if(value.length< {$this->min}) { { $js .= " if(value.length> {$this->max}) { - messages.push(" . CJSON::encode($tooLong) . "); + messages.push(" . json_encode($tooLong) . "); } "; } @@ -145,7 +145,7 @@ if(value.length> {$this->max}) { { $js .= " if(value.length!= {$this->is}) { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; } diff --git a/framework/validators/TypeValidator.php b/framework/validators/TypeValidator.php index 6429263..223843b 100644 --- a/framework/validators/TypeValidator.php +++ b/framework/validators/TypeValidator.php @@ -35,7 +35,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CTypeValidator extends CValidator +class CTypeValidator extends Validator { /** * @var string the data type that the attribute should be. Defaults to 'string'. @@ -75,7 +75,7 @@ class CTypeValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) diff --git a/framework/validators/UniqueValidator.php b/framework/validators/UniqueValidator.php index 7270505..0291b3c 100644 --- a/framework/validators/UniqueValidator.php +++ b/framework/validators/UniqueValidator.php @@ -17,7 +17,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CUniqueValidator extends CValidator +class CUniqueValidator extends Validator { /** * @var boolean whether the comparison is case sensitive. Defaults to true. @@ -72,7 +72,7 @@ class CUniqueValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) diff --git a/framework/validators/UnsafeValidator.php b/framework/validators/UnsafeValidator.php index fa22be4..070afb6 100644 --- a/framework/validators/UnsafeValidator.php +++ b/framework/validators/UnsafeValidator.php @@ -17,7 +17,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CUnsafeValidator extends CValidator +class CUnsafeValidator extends Validator { /** * @var boolean whether attributes listed with this validator should be considered safe for massive assignment. @@ -31,7 +31,7 @@ class CUnsafeValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { } } diff --git a/framework/validators/UrlValidator.php b/framework/validators/UrlValidator.php index 066587a..8856651 100644 --- a/framework/validators/UrlValidator.php +++ b/framework/validators/UrlValidator.php @@ -17,7 +17,7 @@ namespace yii\validators; * @package system.validators * @since 1.0 */ -class CUrlValidator extends CValidator +class CUrlValidator extends Validator { /** * @var string the regular expression used to validate the attribute value. @@ -50,7 +50,7 @@ class CUrlValidator extends CValidator * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ - protected function validateAttribute($object, $attribute) + public function validateAttribute($object, $attribute) { $value = $object->$attribute; if ($this->allowEmpty && $this->isEmpty($value)) @@ -112,14 +112,14 @@ class CUrlValidator extends CValidator $js = " if(!value.match($pattern)) { - messages.push(" . CJSON::encode($message) . "); + messages.push(" . json_encode($message) . "); } "; if ($this->defaultScheme !== null) { $js = " if(!value.match(/:\\/\\//)) { - value=" . CJSON::encode($this->defaultScheme) . "+'://'+value; + value=" . json_encode($this->defaultScheme) . "+'://'+value; } $js "; diff --git a/framework/validators/Validator.php b/framework/validators/Validator.php index 2f5725d..00bb5b5 100644 --- a/framework/validators/Validator.php +++ b/framework/validators/Validator.php @@ -12,50 +12,44 @@ namespace yii\validators; /** * Validator is the base class for all validators. * - * Child classes must implement the {@link validateAttribute} method. + * Child classes may override the [[validateValue]] method to provide the actual + * logic of performing data validation. * - * The following properties are defined in CValidator: - * + * Validator defines the following properties that are common among concrete validators: * - * When using {@link createValidator} to create a validator, the following aliases - * are recognized as the corresponding built-in validator classes: - * + * - [[attributes]]: array, list of attributes to be validated; + * - [[message]]: string, the error message used when validation fails; + * - [[on]]: string, scenarios on which the validator applies. + * + * Validator also declares a set of [[builtInValidators|built-in validators] which can + * be referenced using short names. They are listed as follows: + * + * - `required`: [[RequiredValidator]] + * - `filter`: [[FilterValidator]] + * - `match`: [[RegularExpressionValidator]] + * - `email`: [[EmailValidator]] + * - `url`: [[UrlValidator]] + * - `unique`: [[UniqueValidator]] + * - `compare`: [[CompareValidator]] + * - `length`: [[StringValidator]] + * - `in`: [[RangeValidator]] + * - `numerical`: [[NumberValidator]] + * - `captcha`: [[CaptchaValidator]] + * - `type`: [[TypeValidator]] + * - `file`: [[FileValidator]] + * - `default`: [[DefaultValueValidator]] + * - `exist`: [[ExistValidator]] + * - `boolean`: [[BooleanValidator]] + * - `date`: [[DateValidator]] + * - `safe`: [[SafeValidator]] + * - `unsafe`: [[UnsafeValidator]] * * @author Qiang Xue * @version $Id: CValidator.php 3160 2011-04-03 01:08:23Z qiang.xue $ * @package system.validators * @since 2.0 */ -abstract class Validator extends \yii\base\Component +class Validator extends \yii\base\Component { /** * @var array list of built-in validators (name => class or configuration) @@ -125,10 +119,14 @@ abstract class Validator extends \yii\base\Component /** * Validates a value. * Child classes should override this method to implement the actual validation logic. + * The default implementation simply returns true. * @param mixed $value the value being validated. * @return boolean whether the value is valid. */ - abstract public function validateValue($value); + public function validateValue($value) + { + return true; + } /** * Validates a single attribute. @@ -265,7 +263,7 @@ abstract class Validator extends \yii\base\Component * @param string $message the error message * @param array $params values for the placeholders in the error message */ - protected function addError($object, $attribute, $message, $params = array()) + public function addError($object, $attribute, $message, $params = array()) { $params['{attribute}'] = $object->getAttributeLabel($attribute); $params['{value}'] = $object->$attribute; @@ -280,7 +278,7 @@ abstract class Validator extends \yii\base\Component * @param boolean $trim whether to perform trimming before checking if the string is empty. Defaults to false. * @return boolean whether the value is empty */ - protected function isEmpty($value, $trim = false) + public function isEmpty($value, $trim = false) { return $value === null || $value === array() || $value === '' || $trim && is_scalar($value) && trim($value) === '';