From 4f1efc74a395b4a8380460df6427fda6d172d489 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 21 Jan 2013 19:20:20 -0500 Subject: [PATCH] validator cleanup. --- framework/validators/EmailValidator.php | 17 ++++++------ framework/validators/ExistValidator.php | 12 ++++----- framework/validators/FilterValidator.php | 5 ++-- framework/validators/InlineValidator.php | 2 ++ framework/validators/RangeValidator.php | 9 ++++--- .../validators/RegularExpressionValidator.php | 12 ++++----- framework/validators/RequiredValidator.php | 7 ++--- framework/validators/SafeValidator.php | 30 ---------------------- framework/validators/UniqueValidator.php | 3 +-- framework/validators/Validator.php | 1 + todo.md | 15 ++++++----- 11 files changed, 45 insertions(+), 68 deletions(-) delete mode 100644 framework/validators/SafeValidator.php diff --git a/framework/validators/EmailValidator.php b/framework/validators/EmailValidator.php index 89e8889..8fd8120 100644 --- a/framework/validators/EmailValidator.php +++ b/framework/validators/EmailValidator.php @@ -29,7 +29,7 @@ class EmailValidator extends Validator */ 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. + * @var boolean whether to allow name in the email address (e.g. "John Smith "). Defaults to false. * @see fullPattern */ public $allowName = false; @@ -78,15 +78,16 @@ class EmailValidator extends Validator 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)); + $valid = is_string($value) && strlen($value) <= 254 + && (preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value)); if ($valid) { $domain = rtrim(substr($value, strpos($value, '@') + 1), '>'); - } - if ($valid && $this->checkMX && function_exists('checkdnsrr')) { - $valid = checkdnsrr($domain, 'MX'); - } - if ($valid && $this->checkPort && function_exists('fsockopen')) { - $valid = fsockopen($domain, 25) !== false; + if ($this->checkMX && function_exists('checkdnsrr')) { + $valid = checkdnsrr($domain, 'MX'); + } + if ($valid && $this->checkPort && function_exists('fsockopen')) { + $valid = fsockopen($domain, 25) !== false; + } } return $valid; } diff --git a/framework/validators/ExistValidator.php b/framework/validators/ExistValidator.php index a635595..be710bd 100644 --- a/framework/validators/ExistValidator.php +++ b/framework/validators/ExistValidator.php @@ -8,6 +8,7 @@ */ namespace yii\validators; +use yii\base\InvalidConfigException; /** * ExistValidator validates that the attribute value exists in a table. @@ -21,9 +22,9 @@ namespace yii\validators; class ExistValidator extends Validator { /** - * @var string the yii\db\ActiveRecord class name or alias of the class + * @var string the ActiveRecord class name or alias of the class * that should be used to look for the attribute value being validated. - * Defaults to null, meaning using the yii\db\ActiveRecord class of + * Defaults to null, meaning using the ActiveRecord class of * the attribute being validated. * @see attributeName */ @@ -47,8 +48,7 @@ class ExistValidator extends Validator * * @param \yii\db\ActiveRecord $object the object being validated * @param string $attribute the attribute being validated - * - * @throws \yii\base\Exception if table doesn't have column specified + * @throws InvalidConfigException if table doesn't have column specified */ public function validateAttribute($object, $attribute) { @@ -62,14 +62,14 @@ class ExistValidator extends Validator $attributeName = ($this->attributeName === null) ? $attribute : $this->attributeName; $table = $className::getTableSchema(); if (($column = $table->getColumn($attributeName)) === null) { - throw new \yii\base\Exception('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(); $query->where(array($column->name => $value)); if (!$query->exists()) { $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} "{value}" is invalid.'); - $this->addError($object, $attribute, $message, array('{value}' => $value)); + $this->addError($object, $attribute, $message); } } } diff --git a/framework/validators/FilterValidator.php b/framework/validators/FilterValidator.php index e4b307a..d20defd 100644 --- a/framework/validators/FilterValidator.php +++ b/framework/validators/FilterValidator.php @@ -8,6 +8,7 @@ */ namespace yii\validators; +use yii\base\InvalidConfigException; /** * FilterValidator converts the attribute value according to a filter. @@ -45,12 +46,12 @@ class FilterValidator extends Validator * 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 \yii\base\Exception if filter property is not a valid callback + * @throws InvalidConfigException if filter property is not a valid callback */ public function validateAttribute($object, $attribute) { if ($this->filter === null) { - throw new \yii\base\Exception('The "filter" property must be specified with a valid callback.'); + throw new InvalidConfigException('The "filter" property must be specified with a valid callback.'); } $object->$attribute = call_user_func($this->filter, $object->$attribute); } diff --git a/framework/validators/InlineValidator.php b/framework/validators/InlineValidator.php index 2758066..e324b4b 100644 --- a/framework/validators/InlineValidator.php +++ b/framework/validators/InlineValidator.php @@ -85,6 +85,8 @@ class InlineValidator extends Validator if ($this->clientValidate !== null) { $method = $this->clientValidate; return $object->$method($attribute); + } else { + return null; } } } \ No newline at end of file diff --git a/framework/validators/RangeValidator.php b/framework/validators/RangeValidator.php index 48c4fe5..b2ff773 100644 --- a/framework/validators/RangeValidator.php +++ b/framework/validators/RangeValidator.php @@ -8,6 +8,7 @@ */ namespace yii\validators; +use yii\base\InvalidConfigException; /** * RangeValidator validates that the attribute value is among a list of values. @@ -45,7 +46,7 @@ class RangeValidator extends Validator * 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 \yii\base\Exception if the "range" property is not an array + * @throws InvalidConfigException if the "range" property is not an array */ public function validateAttribute($object, $attribute) { @@ -54,7 +55,7 @@ class RangeValidator extends Validator return; } if (!is_array($this->range)) { - throw new \yii\base\Exception('The "range" property must be specified as an array.'); + throw new InvalidConfigException('The "range" property must be specified as an array.'); } if (!$this->not && !in_array($value, $this->range, $this->strict)) { $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} should be in the list.'); @@ -70,12 +71,12 @@ class RangeValidator extends Validator * @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. - * @throws \yii\base\Exception if the "range" property is not an array + * @throws InvalidConfigException if the "range" property is not an array */ public function clientValidateAttribute($object, $attribute) { if (!is_array($this->range)) { - throw new \yii\base\Exception('The "range" property must be specified as an array.'); + throw new InvalidConfigException('The "range" property must be specified as an array.'); } if (($message = $this->message) === null) { diff --git a/framework/validators/RegularExpressionValidator.php b/framework/validators/RegularExpressionValidator.php index dfbb476..fbdb062 100644 --- a/framework/validators/RegularExpressionValidator.php +++ b/framework/validators/RegularExpressionValidator.php @@ -77,13 +77,13 @@ class RegularExpressionValidator extends Validator $pattern = $this->pattern; $pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern); - $delim = substr($pattern, 0, 1); - $endpos = strrpos($pattern, $delim, 1); - $flag = substr($pattern, $endpos + 1); - if ($delim !== '/') { - $pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $endpos - 1)) . '/'; + $deliminator = substr($pattern, 0, 1); + $pos = strrpos($pattern, $deliminator, 1); + $flag = substr($pattern, $pos + 1); + if ($deliminator !== '/') { + $pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/'; } else { - $pattern = substr($pattern, 0, $endpos + 1); + $pattern = substr($pattern, 0, $pos + 1); } if (!empty($flag)) { $pattern .= preg_replace('/[^igm]/', '', $flag); diff --git a/framework/validators/RequiredValidator.php b/framework/validators/RequiredValidator.php index f34a866..f0f4bfd 100644 --- a/framework/validators/RequiredValidator.php +++ b/framework/validators/RequiredValidator.php @@ -52,9 +52,10 @@ class RequiredValidator extends Validator } } 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); + $message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be "{requiredValue}".'); + $this->addError($object, $attribute, $message, array( + '{requiredValue}' => $this->requiredValue, + )); } } } diff --git a/framework/validators/SafeValidator.php b/framework/validators/SafeValidator.php deleted file mode 100644 index e3e91a6..0000000 --- a/framework/validators/SafeValidator.php +++ /dev/null @@ -1,30 +0,0 @@ - - * @since 2.0 - */ -class SafeValidator extends Validator -{ - /** - * 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 - */ - public function validateAttribute($object, $attribute) - { - } -} - diff --git a/framework/validators/UniqueValidator.php b/framework/validators/UniqueValidator.php index e97fb09..5d5e603 100644 --- a/framework/validators/UniqueValidator.php +++ b/framework/validators/UniqueValidator.php @@ -64,8 +64,7 @@ class UniqueValidator extends Validator $query->where(array($column->name => $value)); if ($object->getIsNewRecord()) { - // if current $object isn't in the database yet then it's OK just - // to call exists() + // if current $object isn't in the database yet then it's OK just to call exists() $exists = $query->exists(); } else { // if current $object is in the database already we can't use exists() diff --git a/framework/validators/Validator.php b/framework/validators/Validator.php index ad6fbee..a213ae4 100644 --- a/framework/validators/Validator.php +++ b/framework/validators/Validator.php @@ -195,6 +195,7 @@ abstract class Validator extends Component */ public function clientValidateAttribute($object, $attribute) { + return null; } /** diff --git a/todo.md b/todo.md index 0c21fe2..400942e 100644 --- a/todo.md +++ b/todo.md @@ -6,10 +6,16 @@ * key-value-based (should allow storage-specific methods additionally to generic ones) * redis (put it under framework/db/redis or perhaps framework/caching?) - logging - * WebTarget - * ProfileTarget + * WebTarget (TBD after web is in place): should consider using javascript and make it into a toolbar + * ProfileTarget (TBD after web is in place): should consider using javascript and make it into a toolbar - caching * a console command to clear cached data +- validators + * FileValidator: depends on CUploadedFile + * CaptchaValidator: depends on CaptchaAction + * type conversion rules + * CompareValidator::clientValidateAttribute(): search for "CHtml::activeId" + * DateValidator: TBD --- @@ -18,11 +24,6 @@ - Module should be able to define its own configuration including routes. Application should be able to overwrite it. * application * security -- validators - * type conversion rules - * CompareValidator::clientValidateAttribute(): search for "CHtml::activeId" - * FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD - * when getting errors from getErrors it will be good to have which validator (at least type) failed exactly. - built-in console commands + api doc builder * support for markdown syntax