Browse Source

w

tags/2.0.0-beta
Qiang Xue 14 years ago
parent
commit
0c7a75bef7
  1. 4
      framework/validators/BooleanValidator.php
  2. 4
      framework/validators/CaptchaValidator.php
  3. 4
      framework/validators/CompareValidator.php
  4. 2
      framework/validators/DateValidator.php
  5. 2
      framework/validators/DefaultValueValidator.php
  6. 42
      framework/validators/EmailValidator.php
  7. 2
      framework/validators/ExistValidator.php
  8. 6
      framework/validators/FileValidator.php
  9. 2
      framework/validators/FilterValidator.php
  10. 4
      framework/validators/NumberValidator.php
  11. 4
      framework/validators/RangeValidator.php
  12. 47
      framework/validators/RegularExpressionValidator.php
  13. 60
      framework/validators/RequiredValidator.php
  14. 12
      framework/validators/SafeValidator.php
  15. 4
      framework/validators/StringValidator.php
  16. 2
      framework/validators/TypeValidator.php
  17. 2
      framework/validators/UniqueValidator.php
  18. 14
      framework/validators/UnsafeValidator.php
  19. 49
      framework/validators/UrlValidator.php
  20. 49
      framework/validators/Validator.php

4
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

4
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

4
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

2
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)

2
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)

42
framework/validators/EmailValidator.php

@ -1,6 +1,6 @@
<?php
/**
* CEmailValidator class file.
* EmailValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,14 +10,12 @@
namespace yii\validators;
/**
* CEmailValidator validates that the attribute value is a valid email address.
* EmailValidator validates that the attribute value is a valid email address.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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 <qiang.xue@gmail.com>"). 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 . ") {

2
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)

6
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)

2
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)

4
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

4
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

47
framework/validators/RegularExpressionValidator.php

@ -1,6 +1,6 @@
<?php
/**
* CRegularExpressionValidator class file.
* RegularExpressionValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,15 +10,14 @@
namespace yii\validators;
/**
* CRegularExpressionValidator validates that the attribute value matches to the specified {@link pattern regular expression}.
* You may invert the validation logic with help of the {@link not} property (available since 1.1.5).
* RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
*
* If [[not]] is set true, the validator will ensure the attribute value do NOT match the [[pattern]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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,12 +77,15 @@ 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)) {

60
framework/validators/RequiredValidator.php

@ -37,69 +37,45 @@ 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.');
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) . ") {
@ -107,10 +83,10 @@ if(value!=" . json_encode($this->requiredValue) . ") {
}
";
}
else
{
if ($message === null)
else {
if ($message === null) {
$message = Yii::t('yii', '{attribute} cannot be blank.');
}
$message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
));

12
framework/validators/SafeValidator.php

@ -1,6 +1,6 @@
<?php
/**
* CSafeValidator class file.
* SafeValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,19 +10,17 @@
namespace yii\validators;
/**
* CSafeValidator marks the associated attributes to be safe for massive assignments.
* SafeValidator marks the associated attributes to be safe for massive assignments.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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)

4
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

2
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)

2
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)

14
framework/validators/UnsafeValidator.php

@ -1,6 +1,6 @@
<?php
/**
* CUnsafeValidator class file.
* UnsafeValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,25 +10,23 @@
namespace yii\validators;
/**
* CUnsafeValidator marks the associated attributes to be unsafe so that they cannot be massively assigned.
* UnsafeValidator marks the associated attributes to be unsafe so that they cannot be massively assigned.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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)

49
framework/validators/UrlValidator.php

@ -1,6 +1,6 @@
<?php
/**
* CUrlValidator class file.
* UrlValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,32 +10,28 @@
namespace yii\validators;
/**
* CUrlValidator validates that the attribute value is a valid http or https URL.
* UrlValidator validates that the attribute value is a valid http or https URL.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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)
{

49
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 <qiang.xue@gmail.com>
* @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.

Loading…
Cancel
Save