diff --git a/framework/validators/BooleanValidator.php b/framework/validators/BooleanValidator.php index 040553b..e15d9d3 100644 --- a/framework/validators/BooleanValidator.php +++ b/framework/validators/BooleanValidator.php @@ -42,7 +42,7 @@ class CBooleanValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) @@ -63,7 +63,7 @@ class CBooleanValidator extends Validator /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. * @see CActiveForm::enableClientValidation diff --git a/framework/validators/CaptchaValidator.php b/framework/validators/CaptchaValidator.php index 101510b..e755d8b 100644 --- a/framework/validators/CaptchaValidator.php +++ b/framework/validators/CaptchaValidator.php @@ -40,7 +40,7 @@ class CCaptchaValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) @@ -82,7 +82,7 @@ class CCaptchaValidator extends Validator /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. * @see CActiveForm::enableClientValidation diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index 6a02d9f..45f3207 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -69,7 +69,7 @@ class CCompareValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) @@ -138,7 +138,7 @@ class CCompareValidator extends Validator /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. * @see CActiveForm::enableClientValidation diff --git a/framework/validators/DateValidator.php b/framework/validators/DateValidator.php index f7f079d..6fb2b2b 100644 --- a/framework/validators/DateValidator.php +++ b/framework/validators/DateValidator.php @@ -44,7 +44,7 @@ class CDateValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) diff --git a/framework/validators/DefaultValueValidator.php b/framework/validators/DefaultValueValidator.php index 1f00465..971a18d 100644 --- a/framework/validators/DefaultValueValidator.php +++ b/framework/validators/DefaultValueValidator.php @@ -34,7 +34,7 @@ class CDefaultValueValidator extends Validator /** * Validates the attribute of 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 */ public function validateAttribute($object, $attribute) diff --git a/framework/validators/EmailValidator.php b/framework/validators/EmailValidator.php index 41a5f55..0052d3f 100644 --- a/framework/validators/EmailValidator.php +++ b/framework/validators/EmailValidator.php @@ -1,6 +1,6 @@ - * @version $Id: CEmailValidator.php 3242 2011-05-28 14:31:04Z qiang.xue $ - * @package system.validators - * @since 1.0 + * @since 2.0 */ -class CEmailValidator extends Validator +class EmailValidator extends Validator { /** * @var string the regular expression used to validate the attribute value. @@ -26,14 +24,12 @@ class CEmailValidator extends Validator public $pattern = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/'; /** * @var string the regular expression used to validate email addresses with the name part. - * This property is used only when {@link allowName} is true. - * @since 1.0.5 + * This property is used only when [[allowName]] is true. * @see allowName */ public $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/'; /** * @var boolean whether to allow name in the email address (e.g. "Qiang Xue "). Defaults to false. - * @since 1.0.5 * @see fullPattern */ public $allowName = false; @@ -46,7 +42,6 @@ class CEmailValidator extends Validator /** * @var boolean whether to check port 25 for the email address. * Defaults to false. - * @since 1.0.4 */ public $checkPort = false; /** @@ -58,16 +53,16 @@ class CEmailValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) { $value = $object->$attribute; - if ($this->allowEmpty && $this->isEmpty($value)) + if ($this->allowEmpty && $this->isEmpty($value)) { return; - if (!$this->validateValue($value)) - { + } + 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); } @@ -75,32 +70,32 @@ class CEmailValidator extends Validator /** * Validates a static value to see if it is a valid email. - * Note that this method does not respect {@link allowEmpty} property. + * Note that this method does not respect [[allowEmpty]] property. * This method is provided so that you can call it directly without going through the model validation rule mechanism. * @param mixed $value the value to be validated * @return boolean whether the value is a valid email - * @since 1.1.1 */ public function validateValue($value) { // make sure string length is limited to avoid DOS attacks $valid = is_string($value) && strlen($value) <= 254 && (preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value)); - if ($valid) + if ($valid) { $domain = rtrim(substr($value, strpos($value, '@') + 1), '>'); - if ($valid && $this->checkMX && function_exists('checkdnsrr')) + } + if ($valid && $this->checkMX && function_exists('checkdnsrr')) { $valid = checkdnsrr($domain, 'MX'); - if ($valid && $this->checkPort && function_exists('fsockopen')) + } + if ($valid && $this->checkPort && function_exists('fsockopen')) { $valid = fsockopen($domain, 25) !== false; + } return $valid; } /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. - * @see CActiveForm::enableClientValidation - * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { @@ -110,8 +105,9 @@ class CEmailValidator extends Validator )); $condition = "!value.match( {$this->pattern})"; - if ($this->allowName) + if ($this->allowName) { $condition .= " && !value.match( {$this->fullPattern})"; + } return " if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { diff --git a/framework/validators/ExistValidator.php b/framework/validators/ExistValidator.php index e82a911..bb68a10 100644 --- a/framework/validators/ExistValidator.php +++ b/framework/validators/ExistValidator.php @@ -53,7 +53,7 @@ class CExistValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) diff --git a/framework/validators/FileValidator.php b/framework/validators/FileValidator.php index 0b1a3fb..95ad356 100644 --- a/framework/validators/FileValidator.php +++ b/framework/validators/FileValidator.php @@ -103,7 +103,7 @@ class CFileValidator extends Validator /** * Set the attribute and then validates using {@link validateFile}. * 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 */ public function validateAttribute($object, $attribute) @@ -139,7 +139,7 @@ class CFileValidator extends Validator /** * Internally validates a file object. - * @param CModel $object the object being validated + * @param \yii\base\Model $object the object being validated * @param string $attribute the attribute being validated * @param CUploadedFile $file uploaded file passed to check against a set of rules */ @@ -183,7 +183,7 @@ class CFileValidator extends Validator /** * Raises an error to inform end user about blank attribute. - * @param CModel $object the object being validated + * @param \yii\base\Model $object the object being validated * @param string $attribute the attribute being validated */ public function emptyAttribute($object, $attribute) diff --git a/framework/validators/FilterValidator.php b/framework/validators/FilterValidator.php index ebffe85..82c50a9 100644 --- a/framework/validators/FilterValidator.php +++ b/framework/validators/FilterValidator.php @@ -38,7 +38,7 @@ class CFilterValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) diff --git a/framework/validators/NumberValidator.php b/framework/validators/NumberValidator.php index bc19287..2905531 100644 --- a/framework/validators/NumberValidator.php +++ b/framework/validators/NumberValidator.php @@ -59,7 +59,7 @@ class CNumberValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) @@ -97,7 +97,7 @@ class CNumberValidator extends Validator /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. * @see CActiveForm::enableClientValidation diff --git a/framework/validators/RangeValidator.php b/framework/validators/RangeValidator.php index 4a6ec1e..e6714e3 100644 --- a/framework/validators/RangeValidator.php +++ b/framework/validators/RangeValidator.php @@ -43,7 +43,7 @@ class CRangeValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) @@ -67,7 +67,7 @@ class CRangeValidator extends Validator /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. * @see CActiveForm::enableClientValidation diff --git a/framework/validators/RegularExpressionValidator.php b/framework/validators/RegularExpressionValidator.php index dea4a8b..437fdd7 100644 --- a/framework/validators/RegularExpressionValidator.php +++ b/framework/validators/RegularExpressionValidator.php @@ -1,6 +1,6 @@ - * @version $Id: CRegularExpressionValidator.php 3120 2011-03-25 01:50:48Z qiang.xue $ - * @package system.validators - * @since 1.0 + * @since 2.0 */ -class CRegularExpressionValidator extends Validator +class RegularExpressionValidator extends Validator { /** * @var string the regular expression to be matched with @@ -31,26 +30,26 @@ class CRegularExpressionValidator extends Validator public $allowEmpty = true; /** * @var boolean whether to invert the validation logic. Defaults to false. If set to true, - * the regular expression defined via {@link pattern} should NOT match the attribute value. - * @since 1.1.5 + * the regular expression defined via [[pattern]] should NOT match the attribute value. **/ public $not = false; /** * 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 */ public function validateAttribute($object, $attribute) { $value = $object->$attribute; - if ($this->allowEmpty && $this->isEmpty($value)) + if ($this->allowEmpty && $this->isEmpty($value)) { return; - if ($this->pattern === null) - throw new CException(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.')); - if ((!$this->not && !preg_match($this->pattern, $value)) || ($this->not && preg_match($this->pattern, $value))) - { + } + if ($this->pattern === null) { + throw new \yii\base\Exception(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.')); + } + if ((!$this->not && !preg_match($this->pattern, $value)) || ($this->not && preg_match($this->pattern, $value))) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is invalid.'); $this->addError($object, $attribute, $message); } @@ -58,16 +57,15 @@ class CRegularExpressionValidator extends Validator /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. - * @see CActiveForm::enableClientValidation - * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { - if ($this->pattern === null) - throw new CException(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.')); + if ($this->pattern === null) { + throw new \yii\base\Exception(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.')); + } $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is invalid.'); $message = strtr($message, array( @@ -79,17 +77,20 @@ class CRegularExpressionValidator extends Validator $delim = substr($pattern, 0, 1); $endpos = strrpos($pattern, $delim, 1); $flag = substr($pattern, $endpos + 1); - if ($delim !== '/') + if ($delim !== '/') { $pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $endpos - 1)) . '/'; - else + } + else { $pattern = substr($pattern, 0, $endpos + 1); - if (!empty($flag)) + } + if (!empty($flag)) { $pattern .= preg_replace('/[^igm]/', '', $flag); + } return " -if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { +if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { messages.push(" . json_encode($message) . "); } "; } -} \ No newline at end of file +} diff --git a/framework/validators/RequiredValidator.php b/framework/validators/RequiredValidator.php index 3ed1ade..56970d0 100644 --- a/framework/validators/RequiredValidator.php +++ b/framework/validators/RequiredValidator.php @@ -37,85 +37,61 @@ class RequiredValidator extends Validator 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 \yii\base\Model $object the object being validated * @param string $attribute the attribute being validated */ public function validateAttribute($object, $attribute) { $value = $object->$attribute; - 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)); + 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); } } - elseif ($this->isEmpty($value, true)) - { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.'); - $this->addError($object, $attribute, $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}".', + array('{requiredValue}' => $this->requiredValue)); + $this->addError($object, $attribute, $message); + } } } /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. - * @see CActiveForm::enableClientValidation - * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $message = $this->message; - if ($this->requiredValue !== null) - { - if ($message === null) - $message = Yii::t('yii', '{attribute} must be {value}.'); + if ($this->requiredValue !== null) { + if ($message === null) { + $message = Yii::t('yii', '{attribute} must be "{requiredValue}".'); + } $message = strtr($message, array( - '{value}' => $this->requiredValue, '{attribute}' => $object->getAttributeLabel($attribute), + '{requiredValue}' => $this->requiredValue, )); return " -if(value!=" . json_encode($this->requiredValue) . ") { +if (value != " . json_encode($this->requiredValue) . ") { messages.push(" . json_encode($message) . "); } "; } - else - { - if ($message === null) + else { + if ($message === null) { $message = Yii::t('yii', '{attribute} cannot be blank.'); + } $message = strtr($message, array( '{attribute}' => $object->getAttributeLabel($attribute), )); return " -if($.trim(value)=='') { +if($.trim(value) == '') { messages.push(" . json_encode($message) . "); } "; diff --git a/framework/validators/SafeValidator.php b/framework/validators/SafeValidator.php index 7119977..b03b45a 100644 --- a/framework/validators/SafeValidator.php +++ b/framework/validators/SafeValidator.php @@ -1,6 +1,6 @@ - * @version $Id: CSafeValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $ - * @package system.validators - * @since 1.1 + * @since 2.0 */ -class CSafeValidator extends Validator +class SafeValidator extends Validator { /** * 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 */ public function validateAttribute($object, $attribute) diff --git a/framework/validators/StringValidator.php b/framework/validators/StringValidator.php index 33a5297..f9a6f4a 100644 --- a/framework/validators/StringValidator.php +++ b/framework/validators/StringValidator.php @@ -60,7 +60,7 @@ class CStringValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) @@ -93,7 +93,7 @@ class CStringValidator extends Validator /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. * @see CActiveForm::enableClientValidation diff --git a/framework/validators/TypeValidator.php b/framework/validators/TypeValidator.php index 223843b..c5d58b1 100644 --- a/framework/validators/TypeValidator.php +++ b/framework/validators/TypeValidator.php @@ -72,7 +72,7 @@ class CTypeValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) diff --git a/framework/validators/UniqueValidator.php b/framework/validators/UniqueValidator.php index 0291b3c..c6856c1 100644 --- a/framework/validators/UniqueValidator.php +++ b/framework/validators/UniqueValidator.php @@ -69,7 +69,7 @@ class CUniqueValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) diff --git a/framework/validators/UnsafeValidator.php b/framework/validators/UnsafeValidator.php index 070afb6..04428fa 100644 --- a/framework/validators/UnsafeValidator.php +++ b/framework/validators/UnsafeValidator.php @@ -1,6 +1,6 @@ - * @version $Id: CUnsafeValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $ - * @package system.validators - * @since 1.0 + * @since 2.0 */ -class CUnsafeValidator extends Validator +class UnsafeValidator extends Validator { /** * @var boolean whether attributes listed with this validator should be considered safe for massive assignment. * Defaults to false. - * @since 1.1.4 */ public $safe = false; + /** * 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 */ public function validateAttribute($object, $attribute) diff --git a/framework/validators/UrlValidator.php b/framework/validators/UrlValidator.php index 8856651..817ad83 100644 --- a/framework/validators/UrlValidator.php +++ b/framework/validators/UrlValidator.php @@ -1,6 +1,6 @@ - * @version $Id: CUrlValidator.php 3242 2011-05-28 14:31:04Z qiang.xue $ - * @package system.validators - * @since 1.0 + * @since 2.0 */ -class CUrlValidator extends Validator +class UrlValidator extends Validator { /** * @var string the regular expression used to validate the attribute value. - * Since version 1.1.7 the pattern may contain a {schemes} token that will be replaced - * by a regular expression which represents the {@see validSchemes}. + * The pattern may contain a `{schemes}` token that will be replaced + * by a regular expression which represents the [[validSchemes]]. */ public $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i'; /** * @var array list of URI schemes which should be considered valid. By default, http and https * are considered to be valid schemes. - * @since 1.1.7 **/ public $validSchemes = array('http', 'https'); /** * @var string the default URI scheme. If the input doesn't contain the scheme part, the default * scheme will be prepended to it (thus changing the input). Defaults to null, meaning a URL must * contain the scheme part. - * @since 1.1.7 **/ public $defaultScheme; /** @@ -47,18 +43,19 @@ class CUrlValidator extends Validator /** * 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 */ public function validateAttribute($object, $attribute) { $value = $object->$attribute; - if ($this->allowEmpty && $this->isEmpty($value)) + if ($this->allowEmpty && $this->isEmpty($value)) { return; - if (($value = $this->validateValue($value)) !== false) + } + if (($value = $this->validateValue($value)) !== false) { $object->$attribute = $value; - else - { + } + else { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid URL.'); $this->addError($object, $attribute, $message); } @@ -66,37 +63,39 @@ class CUrlValidator extends Validator /** * Validates a static value to see if it is a valid URL. - * Note that this method does not respect {@link allowEmpty} property. + * Note that this method does not respect [[allowEmpty]] property. * This method is provided so that you can call it directly without going through the model validation rule mechanism. * @param mixed $value the value to be validated * @return mixed false if the the value is not a valid URL, otherwise the possibly modified value ({@see defaultScheme}) - * @since 1.1.1 */ public function validateValue($value) { - if (is_string($value) && strlen($value) < 2000) // make sure the length is limited to avoid DOS attacks - { - if ($this->defaultScheme !== null && strpos($value, '://') === false) + // make sure the length is limited to avoid DOS attacks + if (is_string($value) && strlen($value) < 2000) { + if ($this->defaultScheme !== null && strpos($value, '://') === false) { $value = $this->defaultScheme . '://' . $value; + } - if (strpos($this->pattern, '{schemes}') !== false) + if (strpos($this->pattern, '{schemes}') !== false) { $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern); - else + } + else { $pattern = $this->pattern; + } - if (preg_match($pattern, $value)) + if (preg_match($pattern, $value)) { return $value; + } } return false; } /** * Returns the JavaScript needed for performing client-side validation. - * @param CModel $object the data object being validated + * @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. * @see CActiveForm::enableClientValidation - * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { diff --git a/framework/validators/Validator.php b/framework/validators/Validator.php index 00bb5b5..d115561 100644 --- a/framework/validators/Validator.php +++ b/framework/validators/Validator.php @@ -12,8 +12,9 @@ namespace yii\validators; /** * Validator is the base class for all validators. * - * Child classes may override the [[validateValue]] method to provide the actual - * logic of performing data validation. + * Child classes must override the [[validateAttribute]] method to provide the actual + * logic of performing data validation. Child classes may also override [[clientValidateAttribute]] + * to provide client-side validation support. * * Validator defines the following properties that are common among concrete validators: * @@ -45,42 +46,39 @@ namespace yii\validators; * - `unsafe`: [[UnsafeValidator]] * * @author Qiang Xue - * @version $Id: CValidator.php 3160 2011-04-03 01:08:23Z qiang.xue $ - * @package system.validators * @since 2.0 */ -class Validator extends \yii\base\Component +abstract class Validator extends \yii\base\Component { /** * @var array list of built-in validators (name => class or configuration) */ public static $builtInValidators = array( 'required' => '\yii\validators\RequiredValidator', - 'filter' => '\yii\validators\FilterValidator', 'match' => '\yii\validators\RegularExpressionValidator', 'email' => '\yii\validators\EmailValidator', 'url' => '\yii\validators\UrlValidator', - 'compare' => '\yii\validators\CompareValidator', - 'length' => '\yii\validators\StringValidator', - 'in' => '\yii\validators\RangeValidator', - 'numerical' => '\yii\validators\NumberValidator', + 'safe' => '\yii\validators\SafeValidator', + 'unsafe' => '\yii\validators\UnsafeValidator', + 'numerical' => '\yii\validators\NumberValidator', 'boolean' => '\yii\validators\BooleanValidator', 'integer' => '\yii\validators\IntegerValidator', 'float' => '\yii\validators\FloatValidator', 'string' => '\yii\validators\StringValidator', 'date' => '\yii\validators\DateValidator', + 'file' => '\yii\validators\FileValidator', + 'filter' => '\yii\validators\FilterValidator', + 'compare' => '\yii\validators\CompareValidator', + 'length' => '\yii\validators\StringValidator', + 'in' => '\yii\validators\RangeValidator', 'captcha' => '\yii\validators\CaptchaValidator', 'type' => '\yii\validators\TypeValidator', - 'file' => '\yii\validators\FileValidator', 'default' => '\yii\validators\DefaultValueValidator', - 'unique' => '\yii\validators\UniqueValidator', 'exist' => '\yii\validators\ExistValidator', - 'safe' => '\yii\validators\SafeValidator', - 'unsafe' => '\yii\validators\UnsafeValidator', ); /** @@ -117,31 +115,12 @@ class Validator extends \yii\base\Component public $enableClientValidation = true; /** - * 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. - */ - public function validateValue($value) - { - return true; - } - - /** * Validates a single attribute. - * The default implementation will call [[validateValue]] to determine if - * the attribute value is valid or not. If not, the [[message|error message]] - * will be added to the model object. + * Child classes must implement this method to provide the actual validation logic. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. */ - public function validateAttribute($object, $attribute) - { - if (!$this->validateValue($object->$attribute)) { - $this->addError($object, $attribute, $this->message); - } - } + abstract public function validateAttribute($object, $attribute); /** * Creates a validator object.