|
|
@ -26,8 +26,8 @@ use yii\validators\RequiredValidator; |
|
|
|
* |
|
|
|
* |
|
|
|
* Model also raises the following events when performing data validation: |
|
|
|
* Model also raises the following events when performing data validation: |
|
|
|
* |
|
|
|
* |
|
|
|
* - [[beforeValidate]]: an event raised at the beginning of [[validate()]] |
|
|
|
* - [[EVENT_BEFORE_VALIDATE]]: an event raised at the beginning of [[validate()]] |
|
|
|
* - [[afterValidate]]: an event raised at the end of [[validate()]] |
|
|
|
* - [[EVENT_AFTER_VALIDATE]]: an event raised at the end of [[validate()]] |
|
|
|
* |
|
|
|
* |
|
|
|
* You may directly use Model to store model data, or extend it with customization. |
|
|
|
* You may directly use Model to store model data, or extend it with customization. |
|
|
|
* You may also customize Model by attaching [[ModelBehavior|model behaviors]]. |
|
|
|
* You may also customize Model by attaching [[ModelBehavior|model behaviors]]. |
|
|
@ -53,9 +53,17 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
const EVENT_AFTER_VALIDATE = 'afterValidate'; |
|
|
|
const EVENT_AFTER_VALIDATE = 'afterValidate'; |
|
|
|
|
|
|
|
|
|
|
|
private static $_attributes = array(); // class name => array of attribute names |
|
|
|
/** |
|
|
|
private $_errors; // attribute name => array of errors |
|
|
|
* @var array validation errors (attribute name => array of errors) |
|
|
|
private $_validators; // Vector of validators |
|
|
|
*/ |
|
|
|
|
|
|
|
private $_errors; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var Vector vector of validators |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private $_validators; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var string current scenario |
|
|
|
|
|
|
|
*/ |
|
|
|
private $_scenario = 'default'; |
|
|
|
private $_scenario = 'default'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -68,10 +76,10 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* array( |
|
|
|
* array( |
|
|
|
* 'attribute list', |
|
|
|
* 'attribute list', |
|
|
|
* 'validator type', |
|
|
|
* 'validator type', |
|
|
|
* 'on'=>'scenario name', |
|
|
|
* 'on'=>'scenario name', |
|
|
|
* ...other parameters... |
|
|
|
* ...other parameters... |
|
|
|
* ) |
|
|
|
* ) |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
@ -79,37 +87,37 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess |
|
|
|
* |
|
|
|
* |
|
|
|
* - attribute list: required, specifies the attributes (separated by commas) to be validated; |
|
|
|
* - attribute list: required, specifies the attributes (separated by commas) to be validated; |
|
|
|
* - validator type: required, specifies the validator to be used. It can be the name of a model |
|
|
|
* - validator type: required, specifies the validator to be used. It can be the name of a model |
|
|
|
* class method, the name of a built-in validator, or a validator class name (or its path alias). |
|
|
|
* class method, the name of a built-in validator, or a validator class name (or its path alias). |
|
|
|
* - on: optional, specifies the [[scenario|scenarios]] (separated by commas) when the validation |
|
|
|
* - on: optional, specifies the [[scenario|scenarios]] (separated by commas) when the validation |
|
|
|
* rule can be applied. If this option is not set, the rule will apply to all scenarios. |
|
|
|
* rule can be applied. If this option is not set, the rule will apply to all scenarios. |
|
|
|
* - additional name-value pairs can be specified to initialize the corresponding validator properties. |
|
|
|
* - additional name-value pairs can be specified to initialize the corresponding validator properties. |
|
|
|
* Please refer to individual validator class API for possible properties. |
|
|
|
* Please refer to individual validator class API for possible properties. |
|
|
|
* |
|
|
|
* |
|
|
|
* A validator can be either an object of a class extending [[\yii\validators\Validator]], |
|
|
|
* A validator can be either an object of a class extending [[Validator]], or a model class method |
|
|
|
* or a model class method (called *inline validator*) that has the following signature: |
|
|
|
* (called *inline validator*) that has the following signature: |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* // $params refers to validation parameters given in the rule |
|
|
|
* // $params refers to validation parameters given in the rule |
|
|
|
* function validatorName($attribute, $params) |
|
|
|
* function validatorName($attribute, $params) |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* Yii also provides a set of [[\yii\validators\Validator::builtInValidators|built-in validators]]. |
|
|
|
* Yii also provides a set of [[Validator::builtInValidators|built-in validators]]. |
|
|
|
* They each has an alias name which can be used when specifying a validation rule. |
|
|
|
* They each has an alias name which can be used when specifying a validation rule. |
|
|
|
* |
|
|
|
* |
|
|
|
* Below are some examples: |
|
|
|
* Below are some examples: |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* array( |
|
|
|
* array( |
|
|
|
* // built-in "required" validator |
|
|
|
* // built-in "required" validator |
|
|
|
* array('username', 'required'), |
|
|
|
* array('username', 'required'), |
|
|
|
* // built-in "length" validator customized with "min" and "max" properties |
|
|
|
* // built-in "length" validator customized with "min" and "max" properties |
|
|
|
* array('username', 'length', 'min'=>3, 'max'=>12), |
|
|
|
* array('username', 'length', 'min'=>3, 'max'=>12), |
|
|
|
* // built-in "compare" validator that is used in "register" scenario only |
|
|
|
* // built-in "compare" validator that is used in "register" scenario only |
|
|
|
* array('password', 'compare', 'compareAttribute'=>'password2', 'on'=>'register'), |
|
|
|
* array('password', 'compare', 'compareAttribute'=>'password2', 'on'=>'register'), |
|
|
|
* // an inline validator defined via the "authenticate()" method in the model class |
|
|
|
* // an inline validator defined via the "authenticate()" method in the model class |
|
|
|
* array('password', 'authenticate', 'on'=>'login'), |
|
|
|
* array('password', 'authenticate', 'on'=>'login'), |
|
|
|
* // a validator of class "CaptchaValidator" |
|
|
|
* // a validator of class "CaptchaValidator" |
|
|
|
* array('captcha', 'CaptchaValidator'), |
|
|
|
* array('captcha', 'CaptchaValidator'), |
|
|
|
* ); |
|
|
|
* ); |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
@ -151,8 +159,10 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess |
|
|
|
{ |
|
|
|
{ |
|
|
|
$attributes = array(); |
|
|
|
$attributes = array(); |
|
|
|
foreach ($this->getActiveValidators() as $validator) { |
|
|
|
foreach ($this->getActiveValidators() as $validator) { |
|
|
|
foreach ($validator->attributes as $name) { |
|
|
|
if ($validator->isActive('default')) { |
|
|
|
$attributes[$name] = true; |
|
|
|
foreach ($validator->attributes as $name) { |
|
|
|
|
|
|
|
$attributes[$name] = true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return array( |
|
|
|
return array( |
|
|
@ -168,11 +178,6 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function attributes() |
|
|
|
public function attributes() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$className = get_class($this); |
|
|
|
|
|
|
|
if (isset(self::$_attributes[$className])) { |
|
|
|
|
|
|
|
return self::$_attributes[$className]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$class = new \ReflectionClass($this); |
|
|
|
$class = new \ReflectionClass($this); |
|
|
|
$names = array(); |
|
|
|
$names = array(); |
|
|
|
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
|
|
|
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
|
|
@ -181,7 +186,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess |
|
|
|
$names[] = $name; |
|
|
|
$names[] = $name; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return self::$_attributes[$className] = $names; |
|
|
|
return $names; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -395,13 +400,13 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* array( |
|
|
|
* array( |
|
|
|
* 'username' => array( |
|
|
|
* 'username' => array( |
|
|
|
* 'Username is required.', |
|
|
|
* 'Username is required.', |
|
|
|
* 'Username must contain only word characters.', |
|
|
|
* 'Username must contain only word characters.', |
|
|
|
* ), |
|
|
|
* ), |
|
|
|
* 'email' => array( |
|
|
|
* 'email' => array( |
|
|
|
* 'Email address is invalid.', |
|
|
|
* 'Email address is invalid.', |
|
|
|
* ) |
|
|
|
* ) |
|
|
|
* ) |
|
|
|
* ) |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|