Browse Source

w

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

8
framework/validators/BooleanValidator.php

@ -17,7 +17,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0.10 * @since 1.0.10
*/ */
class CBooleanValidator extends CValidator class CBooleanValidator extends Validator
{ {
/** /**
* @var mixed the value representing true status. Defaults to '1'. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -78,8 +78,8 @@ class CBooleanValidator extends CValidator
'{false}' => $this->falseValue, '{false}' => $this->falseValue,
)); ));
return " return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . "value!=" . CJSON::encode($this->trueValue) . " && value!=" . CJSON::encode($this->falseValue) . ") { if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . "value!=" . json_encode($this->trueValue) . " && value!=" . json_encode($this->falseValue) . ") {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
} }

8
framework/validators/CaptchaValidator.php

@ -19,7 +19,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CCaptchaValidator extends CValidator class CCaptchaValidator extends Validator
{ {
/** /**
* @var boolean whether the comparison is case sensitive. Defaults to false. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -61,7 +61,7 @@ class CCaptchaValidator extends CValidator
* @return CCaptchaAction the action object * @return CCaptchaAction the action object
* @since 1.1.7 * @since 1.1.7
*/ */
protected function getCaptchaAction() public function getCaptchaAction()
{ {
if (($captcha = Yii::app()->getController()->createAction($this->captchaAction)) === null) if (($captcha = Yii::app()->getController()->createAction($this->captchaAction)) === null)
{ {
@ -105,7 +105,7 @@ else
hash = hash[" . ($this->caseSensitive ? 0 : 1) . "]; hash = hash[" . ($this->caseSensitive ? 0 : 1) . "];
for(var i=value.length-1, h=0; i >= 0; --i) h+=value." . ($this->caseSensitive ? '' : 'toLowerCase().') . "charCodeAt(i); for(var i=value.length-1, h=0; i >= 0; --i) h+=value." . ($this->caseSensitive ? '' : 'toLowerCase().') . "charCodeAt(i);
if(h != hash) { if(h != hash) {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";

8
framework/validators/CompareValidator.php

@ -29,7 +29,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CCompareValidator extends CValidator class CCompareValidator extends Validator
{ {
/** /**
* @var string the name of the attribute to be compared with * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -149,7 +149,7 @@ class CCompareValidator extends CValidator
if ($this->compareValue !== null) if ($this->compareValue !== null)
{ {
$compareTo = $this->compareValue; $compareTo = $this->compareValue;
$compareValue = CJSON::encode($this->compareValue); $compareValue = json_encode($this->compareValue);
} }
else else
{ {
@ -203,7 +203,7 @@ class CCompareValidator extends CValidator
return " return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
} }

4
framework/validators/DateValidator.php

@ -20,7 +20,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.1.7 * @since 1.1.7
*/ */
class CDateValidator extends CValidator class CDateValidator extends Validator
{ {
/** /**
* @var mixed the format pattern that the date value should follow. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))

4
framework/validators/DefaultValueValidator.php

@ -19,7 +19,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0.2 * @since 1.0.2
*/ */
class CDefaultValueValidator extends CValidator class CDefaultValueValidator extends Validator
{ {
/** /**
* @var mixed the default value to be set to the specified attributes. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
if (!$this->setOnEmpty) if (!$this->setOnEmpty)
$object->$attribute = $this->value; $object->$attribute = $this->value;

6
framework/validators/EmailValidator.php

@ -17,7 +17,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CEmailValidator extends CValidator class CEmailValidator extends Validator
{ {
/** /**
* @var string the regular expression used to validate the attribute value. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -115,7 +115,7 @@ class CEmailValidator extends CValidator
return " return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
} }

4
framework/validators/ExistValidator.php

@ -20,7 +20,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0.4 * @since 1.0.4
*/ */
class CExistValidator extends CValidator class CExistValidator extends Validator
{ {
/** /**
* @var string the ActiveRecord class name that should be used to * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))

10
framework/validators/FileValidator.php

@ -44,7 +44,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CFileValidator extends CValidator class CFileValidator extends Validator
{ {
/** /**
* @var boolean whether the attribute requires a file to be uploaded or not. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
if ($this->maxFiles > 1) if ($this->maxFiles > 1)
{ {
@ -143,7 +143,7 @@ class CFileValidator extends CValidator
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
* @param CUploadedFile $file uploaded file passed to check against a set of rules * @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) if (null === $file || ($error = $file->getError()) == UPLOAD_ERR_NO_FILE)
return $this->emptyAttribute($object, $attribute); return $this->emptyAttribute($object, $attribute);
@ -186,7 +186,7 @@ class CFileValidator extends CValidator
* @param CModel $object the object being validated * @param CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function emptyAttribute($object, $attribute) public function emptyAttribute($object, $attribute)
{ {
if (!$this->allowEmpty) if (!$this->allowEmpty)
{ {
@ -206,7 +206,7 @@ class CFileValidator extends CValidator
* *
* @return integer the size limit for uploaded files. * @return integer the size limit for uploaded files.
*/ */
protected function getSizeLimit() public function getSizeLimit()
{ {
$limit = ini_get('upload_max_filesize'); $limit = ini_get('upload_max_filesize');
$limit = $this->sizeToBytes($limit); $limit = $this->sizeToBytes($limit);

4
framework/validators/FilterValidator.php

@ -28,7 +28,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CFilterValidator extends CValidator class CFilterValidator extends Validator
{ {
/** /**
* @var callback the filter method * @var callback the filter method
@ -41,7 +41,7 @@ class CFilterValidator extends CValidator
* @param CModel $object the object being validated * @param CModel $object the object being validated
* @param string $attribute the attribute 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)) if ($this->filter === null || !is_callable($this->filter))
throw new CException(Yii::t('yii', 'The "filter" property must be specified with a valid callback.')); throw new CException(Yii::t('yii', 'The "filter" property must be specified with a valid callback.'));

24
framework/validators/InlineValidator.php

@ -1,6 +1,6 @@
<?php <?php
/** /**
* CInlineValidator class file. * InlineValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,14 +10,21 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CInlineValidator represents a validator which is defined as a method in the object being validated. * InlineValidator represents a validator which is defined as a method in the object being validated.
*
* The validation method must have the following signature:
*
* ~~~
* function foo($attribute, $params)
* ~~~
*
* where `$attribute` refers to the name of the attribute being validated, while `$params`
* is an array representing the additional parameters supplied in the validation rule.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CInlineValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $ * @since 2.0
* @package system.validators
* @since 1.0
*/ */
class CInlineValidator extends CValidator class InlineValidator extends Validator
{ {
/** /**
* @var string the name of the validation method defined in the active record class * @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. * 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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$method = $this->method; $method = $this->method;
$object->$method($attribute, $this->params); $object->$method($attribute, $this->params);

10
framework/validators/NumberValidator.php

@ -17,7 +17,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @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. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -130,14 +130,14 @@ class CNumberValidator extends CValidator
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
$js = " $js = "
if(!value.match($pattern)) { if(!value.match($pattern)) {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
if ($this->min !== null) if ($this->min !== null)
{ {
$js .= " $js .= "
if(value< {$this->min}) { if(value< {$this->min}) {
messages.push(" . CJSON::encode($tooSmall) . "); messages.push(" . json_encode($tooSmall) . ");
} }
"; ";
} }
@ -145,7 +145,7 @@ if(value< {$this->min}) {
{ {
$js .= " $js .= "
if(value> {$this->max}) { if(value> {$this->max}) {
messages.push(" . CJSON::encode($tooBig) . "); messages.push(" . json_encode($tooBig) . ");
} }
"; ";
} }

8
framework/validators/RangeValidator.php

@ -18,7 +18,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CRangeValidator extends CValidator class CRangeValidator extends Validator
{ {
/** /**
* @var array list of valid values that the attribute value should be among * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -87,11 +87,11 @@ class CRangeValidator extends CValidator
$range = array(); $range = array();
foreach ($this->range as $value) foreach ($this->range as $value)
$range[] = (string)$value; $range[] = (string)$value;
$range = CJSON::encode($range); $range = json_encode($range);
return " return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? "$.inArray(value, $range)>=0" : "$.inArray(value, $range)<0") . ") { 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) . ");
} }
"; ";
} }

6
framework/validators/RegularExpressionValidator.php

@ -18,7 +18,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CRegularExpressionValidator extends CValidator class CRegularExpressionValidator extends Validator
{ {
/** /**
* @var string the regular expression to be matched with * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -88,7 +88,7 @@ class CRegularExpressionValidator extends CValidator
return " return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
} }

55
framework/validators/RequiredValidator.php

@ -1,6 +1,6 @@
<?php <?php
/** /**
* CRequiredValidator class file. * RequiredValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,39 +10,60 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CRequiredValidator validates that the specified attribute does not have null or empty value. * RequiredValidator validates that the specified attribute does not have null or empty value.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CRequiredValidator.php 3157 2011-04-02 19:21:06Z qiang.xue $ * @since 2.0
* @package system.validators
* @since 1.0
*/ */
class CRequiredValidator extends CValidator class RequiredValidator extends Validator
{ {
/** /**
* @var mixed the desired value that the attribute must have. * @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 * 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. * the attribute has a value that is the same as this property value.
* Defaults to null. * Defaults to null.
* @since 1.0.10 * @see strict
*/ */
public $requiredValue; public $requiredValue;
/** /**
* @var boolean whether the comparison to {@link requiredValue} is strict. * @var boolean whether the comparison between the attribute value and [[requiredValue]] is strict.
* When this is true, the attribute value and type must both match those of {@link requiredValue}. * When this is true, both the values and types must match.
* Defaults to false, meaning only the value needs to be matched. * Defaults to false, meaning only the values need to match.
* This property is only used when {@link requiredValue} is not null. * Note that when [[requiredValue]] is null, if this property is true, the validator will check
* @since 1.0.10 * 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; 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. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->requiredValue !== null) if ($this->requiredValue !== null)
@ -81,8 +102,8 @@ class CRequiredValidator extends CValidator
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
)); ));
return " return "
if(value!=" . CJSON::encode($this->requiredValue) . ") { if(value!=" . json_encode($this->requiredValue) . ") {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
} }
@ -95,7 +116,7 @@ if(value!=" . CJSON::encode($this->requiredValue) . ") {
)); ));
return " return "
if($.trim(value)=='') { if($.trim(value)=='') {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
} }

4
framework/validators/SafeValidator.php

@ -17,7 +17,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.1 * @since 1.1
*/ */
class CSafeValidator extends CValidator class CSafeValidator extends Validator
{ {
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
@ -25,7 +25,7 @@ class CSafeValidator extends CValidator
* @param CModel $object the object being validated * @param CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
} }
} }

10
framework/validators/StringValidator.php

@ -19,7 +19,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CStringValidator extends CValidator class CStringValidator extends Validator
{ {
/** /**
* @var integer maximum length. Defaults to null, meaning no maximum limit. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -129,7 +129,7 @@ class CStringValidator extends CValidator
{ {
$js .= " $js .= "
if(value.length< {$this->min}) { 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 .= " $js .= "
if(value.length> {$this->max}) { 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 .= " $js .= "
if(value.length!= {$this->is}) { if(value.length!= {$this->is}) {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
} }

4
framework/validators/TypeValidator.php

@ -35,7 +35,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CTypeValidator extends CValidator class CTypeValidator extends Validator
{ {
/** /**
* @var string the data type that the attribute should be. Defaults to 'string'. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))

4
framework/validators/UniqueValidator.php

@ -17,7 +17,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CUniqueValidator extends CValidator class CUniqueValidator extends Validator
{ {
/** /**
* @var boolean whether the comparison is case sensitive. Defaults to true. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))

4
framework/validators/UnsafeValidator.php

@ -17,7 +17,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @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. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
} }
} }

8
framework/validators/UrlValidator.php

@ -17,7 +17,7 @@ namespace yii\validators;
* @package system.validators * @package system.validators
* @since 1.0 * @since 1.0
*/ */
class CUrlValidator extends CValidator class CUrlValidator extends Validator
{ {
/** /**
* @var string the regular expression used to validate the attribute value. * @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 CModel $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
protected function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value))
@ -112,14 +112,14 @@ class CUrlValidator extends CValidator
$js = " $js = "
if(!value.match($pattern)) { if(!value.match($pattern)) {
messages.push(" . CJSON::encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
if ($this->defaultScheme !== null) if ($this->defaultScheme !== null)
{ {
$js = " $js = "
if(!value.match(/:\\/\\//)) { if(!value.match(/:\\/\\//)) {
value=" . CJSON::encode($this->defaultScheme) . "+'://'+value; value=" . json_encode($this->defaultScheme) . "+'://'+value;
} }
$js $js
"; ";

76
framework/validators/Validator.php

@ -12,50 +12,44 @@ namespace yii\validators;
/** /**
* Validator is the base class for all 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:
* <ul>
* <li>{@link attributes}: array, list of attributes to be validated;</li>
* <li>{@link message}: string, the customized error message. The message
* may contain placeholders that will be replaced with the actual content.
* For example, the "{attribute}" placeholder will be replaced with the label
* of the problematic attribute. Different validators may define additional
* placeholders.</li>
* <li>{@link on}: string, in which scenario should the validator be in effect.
* This is used to match the 'on' parameter supplied when calling {@link CModel::validate}.</li>
* </ul>
* *
* When using {@link createValidator} to create a validator, the following aliases * - [[attributes]]: array, list of attributes to be validated;
* are recognized as the corresponding built-in validator classes: * - [[message]]: string, the error message used when validation fails;
* <ul> * - [[on]]: string, scenarios on which the validator applies.
* <li>required: {@link CRequiredValidator}</li> *
* <li>filter: {@link CFilterValidator}</li> * Validator also declares a set of [[builtInValidators|built-in validators] which can
* <li>match: {@link CRegularExpressionValidator}</li> * be referenced using short names. They are listed as follows:
* <li>email: {@link CEmailValidator}</li> *
* <li>url: {@link CUrlValidator}</li> * - `required`: [[RequiredValidator]]
* <li>unique: {@link CUniqueValidator}</li> * - `filter`: [[FilterValidator]]
* <li>compare: {@link CCompareValidator}</li> * - `match`: [[RegularExpressionValidator]]
* <li>length: {@link CStringValidator}</li> * - `email`: [[EmailValidator]]
* <li>in: {@link CRangeValidator}</li> * - `url`: [[UrlValidator]]
* <li>numerical: {@link CNumberValidator}</li> * - `unique`: [[UniqueValidator]]
* <li>captcha: {@link CCaptchaValidator}</li> * - `compare`: [[CompareValidator]]
* <li>type: {@link CTypeValidator}</li> * - `length`: [[StringValidator]]
* <li>file: {@link CFileValidator}</li> * - `in`: [[RangeValidator]]
* <li>default: {@link CDefaultValueValidator}</li> * - `numerical`: [[NumberValidator]]
* <li>exist: {@link CExistValidator}</li> * - `captcha`: [[CaptchaValidator]]
* <li>boolean: {@link CBooleanValidator}</li> * - `type`: [[TypeValidator]]
* <li>date: {@link CDateValidator}</li> * - `file`: [[FileValidator]]
* <li>safe: {@link CSafeValidator}</li> * - `default`: [[DefaultValueValidator]]
* <li>unsafe: {@link CUnsafeValidator}</li> * - `exist`: [[ExistValidator]]
* </ul> * - `boolean`: [[BooleanValidator]]
* - `date`: [[DateValidator]]
* - `safe`: [[SafeValidator]]
* - `unsafe`: [[UnsafeValidator]]
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CValidator.php 3160 2011-04-03 01:08:23Z qiang.xue $ * @version $Id: CValidator.php 3160 2011-04-03 01:08:23Z qiang.xue $
* @package system.validators * @package system.validators
* @since 2.0 * @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) * @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. * Validates a value.
* Child classes should override this method to implement the actual validation logic. * 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. * @param mixed $value the value being validated.
* @return boolean whether the value is valid. * @return boolean whether the value is valid.
*/ */
abstract public function validateValue($value); public function validateValue($value)
{
return true;
}
/** /**
* Validates a single attribute. * Validates a single attribute.
@ -265,7 +263,7 @@ abstract class Validator extends \yii\base\Component
* @param string $message the error message * @param string $message the error message
* @param array $params values for the placeholders in 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['{attribute}'] = $object->getAttributeLabel($attribute);
$params['{value}'] = $object->$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. * @param boolean $trim whether to perform trimming before checking if the string is empty. Defaults to false.
* @return boolean whether the value is empty * @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 === '' return $value === null || $value === array() || $value === ''
|| $trim && is_scalar($value) && trim($value) === ''; || $trim && is_scalar($value) && trim($value) === '';

Loading…
Cancel
Save