|
|
@ -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) === ''; |
|
|
|