Commit 7b7f159d by Qiang Xue

w

parent 567f81ce
...@@ -20,7 +20,7 @@ namespace yii\base; ...@@ -20,7 +20,7 @@ namespace yii\base;
class Model extends Component implements \IteratorAggregate, \ArrayAccess class Model extends Component implements \IteratorAggregate, \ArrayAccess
{ {
private static $_attributes = array(); // class name => array of attribute names private static $_attributes = array(); // class name => array of attribute names
private $_errors = array(); // attribute name => array of errors private $_errors; // attribute name => array of errors
private $_validators; // validators private $_validators; // validators
private $_scenario; // scenario private $_scenario; // scenario
...@@ -238,7 +238,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -238,7 +238,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public function beforeValidate() public function beforeValidate()
{ {
if ($this->hasEventHandler('onBeforeValidate')) { if ($this->hasEventHandler('onBeforeValidate')) {
$event = new ModelEvent($this); $event = new ValidationEvent($this);
$this->onBeforeValidate($event); $this->onBeforeValidate($event);
return $event->isValid; return $event->isValid;
} }
...@@ -269,7 +269,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -269,7 +269,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
/** /**
* This event is raised before the validation is performed. * This event is raised before the validation is performed.
* @param ModelEvent $event the event parameter * @param ValidationEvent $event the event parameter
*/ */
public function onBeforeValidate($event) public function onBeforeValidate($event)
{ {
...@@ -384,11 +384,13 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -384,11 +384,13 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public function getAttributeLabel($attribute) public function getAttributeLabel($attribute)
{ {
$labels = $this->attributeLabels(); $labels = $this->attributeLabels();
if (isset($labels[$attribute])) if (isset($labels[$attribute])) {
return $labels[$attribute]; return $labels[$attribute];
else }
else {
return $this->generateAttributeLabel($attribute); return $this->generateAttributeLabel($attribute);
} }
}
/** /**
* Returns a value indicating whether there is any validation error. * Returns a value indicating whether there is any validation error.
...@@ -398,7 +400,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -398,7 +400,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public function hasErrors($attribute = null) public function hasErrors($attribute = null)
{ {
if ($attribute === null) { if ($attribute === null) {
return $this->_errors !== array(); return !empty($this->_errors);
} }
else { else {
return isset($this->_errors[$attribute]); return isset($this->_errors[$attribute]);
...@@ -409,11 +411,26 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -409,11 +411,26 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
* Returns the errors for all attribute or a single attribute. * Returns the errors for all attribute or a single attribute.
* @param string $attribute attribute name. Use null to retrieve errors for all attributes. * @param string $attribute attribute name. Use null to retrieve errors for all attributes.
* @return array errors for all attributes or the specified attribute. Empty array is returned if no error. * @return array errors for all attributes or the specified attribute. Empty array is returned if no error.
* Note that when returning errors for all attributes, the result is a two-dimensional array, like the following:
*
* ~~~php
* array(
* 'username' => array(
* 'Username is required.',
* 'Username must contain only word characters.',
* ),
* 'email' => array(
* 'Email address is invalid.',
* )
* )
* ~~~
*
* @see getError
*/ */
public function getErrors($attribute = null) public function getErrors($attribute = null)
{ {
if ($attribute === null) { if ($attribute === null) {
return $this->_errors; return $this->_errors === null ? array() : $this->_errors;
} }
else { else {
return isset($this->_errors[$attribute]) ? $this->_errors[$attribute] : array(); return isset($this->_errors[$attribute]) ? $this->_errors[$attribute] : array();
...@@ -424,6 +441,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -424,6 +441,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
* Returns the first error of the specified attribute. * Returns the first error of the specified attribute.
* @param string $attribute attribute name. * @param string $attribute attribute name.
* @return string the error message. Null is returned if no error. * @return string the error message. Null is returned if no error.
* @see getErrors
*/ */
public function getError($attribute) public function getError($attribute)
{ {
...@@ -445,21 +463,20 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -445,21 +463,20 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
* @param array $errors a list of errors. The array keys must be attribute names. * @param array $errors a list of errors. The array keys must be attribute names.
* The array values should be error messages. If an attribute has multiple errors, * The array values should be error messages. If an attribute has multiple errors,
* these errors must be given in terms of an array. * these errors must be given in terms of an array.
* You may use the result of {@link getErrors} as the value for this parameter.
*/ */
public function addErrors($errors) public function addErrors($errors)
{ {
foreach ($errors as $attribute => $error) foreach ($errors as $attribute => $error) {
{ if (is_array($error)) {
if (is_array($error)) foreach ($error as $e) {
{
foreach ($error as $e)
$this->_errors[$attribute][] = $e; $this->_errors[$attribute][] = $e;
} }
else }
else {
$this->_errors[$attribute][] = $error; $this->_errors[$attribute][] = $error;
} }
} }
}
/** /**
* Removes errors for all attributes or a single attribute. * Removes errors for all attributes or a single attribute.
...@@ -476,10 +493,10 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -476,10 +493,10 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
} }
/** /**
* Generates a user friendly attribute label. * Generates a user friendly attribute label based on the give attribute name.
* This is done by replacing underscores or dashes with blanks and * This is done by replacing underscores, dashes and dots with blanks and
* changing the first letter of each word to upper case. * changing the first letter of each word to upper case.
* For example, 'department_name' or 'DepartmentName' becomes 'Department Name'. * For example, 'department_name' or 'DepartmentName' will generate 'Department Name'.
* @param string $name the column name * @param string $name the column name
* @return string the attribute label * @return string the attribute label
*/ */
...@@ -489,9 +506,9 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -489,9 +506,9 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
} }
/** /**
* Returns all attribute values. * Returns attribute values.
* @param array $names list of attributes whose value needs to be returned. * @param array $names list of attributes whose value needs to be returned.
* Defaults to null, meaning all attributes as listed in {@link attributeNames} will be returned. * Defaults to null, meaning all attributes listed in [[attributeNames]] will be returned.
* If it is an array, only the attributes in the array will be returned. * If it is an array, only the attributes in the array will be returned.
* @return array attribute values (name=>value). * @return array attribute values (name=>value).
*/ */
...@@ -517,9 +534,9 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -517,9 +534,9 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
/** /**
* Sets the attribute values in a massive way. * Sets the attribute values in a massive way.
* @param array $values attribute values (name=>value) to be set. * @param array $values attribute values (name=>value) to be assigned to the model.
* @param boolean $safeOnly whether the assignments should only be done to the safe attributes. * @param boolean $safeOnly whether the assignments should only be done to the safe attributes.
* A safe attribute is one that is associated with a validation rule in the current {@link scenario}. * A safe attribute is one that is associated with a validation rule in the current [[scenario]].
* @see getSafeAttributeNames * @see getSafeAttributeNames
* @see attributeNames * @see attributeNames
*/ */
...@@ -563,7 +580,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -563,7 +580,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public function onUnsafeAttribute($name, $value) public function onUnsafeAttribute($name, $value)
{ {
if (YII_DEBUG) if (YII_DEBUG)
\Yii::warning(sprintf('Failed to set unsafe attribute "%s" of "%s".', $name, get_class($this)); \Yii::warning(sprintf('Failed to set unsafe attribute "%s" in "%s".', $name, get_class($this));
} }
/** /**
......
<?php <?php
/** /**
* CModelBehavior class file. * ModelBehavior class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\base;
/** /**
* CModelBehavior is a base class for behaviors that are attached to a model component. * ModelBehavior class.
* The model should extend from {@link CModel} or its child classes. *
* ModelBehavior is a base class for behaviors that are attached to a model object.
* The model should be an instance of [[Model]] or its child classes.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CModelBehavior.php 2799 2011-01-01 19:31:13Z qiang.xue $ * @since 2.0
* @package system.base
* @since 1.0.2
*/ */
class CModelBehavior extends CBehavior class ModelBehavior extends Behavior
{ {
/** /**
* Declares events and the corresponding event handler methods. * Declares event handlers for owner's events.
* The default implementation returns 'onBeforeValidate' and 'onAfterValidate' events and handlers. * The default implementation returns three event handlers:
* If you override this method, make sure you merge the parent result to the return value. *
* - `onAfterConstruct` event: [[afterConstruct]]
* - `onBeforeValidate` event: [[beforeValidate]]
* - `onAfterValidate` event: [[afterValidate]]
*
* You may override these event handler methods to respond to the corresponding owner events.
* @return array events (array keys) and the corresponding event handler methods (array values). * @return array events (array keys) and the corresponding event handler methods (array values).
* @see CBehavior::events
*/ */
public function events() public function events()
{ {
...@@ -35,28 +41,29 @@ class CModelBehavior extends CBehavior ...@@ -35,28 +41,29 @@ class CModelBehavior extends CBehavior
} }
/** /**
* Responds to {@link CModel::onAfterConstruct} event. * Responds to [[Model::onAfterConstruct]] event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. * Overrides this method if you want to handle the corresponding event of the [[owner]].
* @param CEvent $event event parameter * @param Event $event event parameter
*/ */
public function afterConstruct($event) public function afterConstruct($event)
{ {
} }
/** /**
* Responds to {@link CModel::onBeforeValidate} event. * Responds to [[Model::onBeforeValidate]] event.
* Overrides this method if you want to handle the corresponding event of the {@link owner}. * Overrides this method if you want to handle the corresponding event of the [[owner]].
* You may set {@link CModelEvent::isValid} to be false to quit the validation process. * You may set the [[ValidationEvent::isValid|isValid]] property of the event parameter
* @param CModelEvent $event event parameter * to be false to cancel the validation process.
* @param ValidationEvent $event event parameter
*/ */
public function beforeValidate($event) public function beforeValidate($event)
{ {
} }
/** /**
* Responds to {@link CModel::onAfterValidate} event. * Responds to [[Model::onAfterValidate]] event.
* Overrides this method if you want to handle the corresponding event of the {@link owner}. * Overrides this method if you want to handle the corresponding event of the [[owner]].
* @param CEvent $event event parameter * @param Event $event event parameter
*/ */
public function afterValidate($event) public function afterValidate($event)
{ {
......
...@@ -20,14 +20,6 @@ namespace yii\base; ...@@ -20,14 +20,6 @@ namespace yii\base;
class ModelEvent extends Event class ModelEvent extends Event
{ {
/** /**
* @var boolean whether the model is in valid status and should continue its normal method execution cycles. Defaults to true.
* For example, when this event is raised in a {@link CFormModel} object that is executing {@link CModel::beforeValidate},
* if this property is set false by the event handler, the {@link CModel::validate} method will quit after handling this event.
* If true, the normal execution cycles will continue, including performing the real validations and calling
* {@link CModel::afterValidate}.
*/
public $isValid = true;
/**
* @var CDbCrireria the query criteria that is passed as a parameter to a find method of {@link CActiveRecord}. * @var CDbCrireria the query criteria that is passed as a parameter to a find method of {@link CActiveRecord}.
* Note that this property is only used by {@link CActiveRecord::onBeforeFind} event. * Note that this property is only used by {@link CActiveRecord::onBeforeFind} event.
* This property could be null. * This property could be null.
......
<?php
/**
* ValidationEvent class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* ValidationEvent class.
*
* ValidationEvent represents the parameter needed by model validation events.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ValidationEvent extends Event
{
/**
* @var boolean whether the model passes the validation by the event handler.
* Defaults to true. If it is set false, the [[Model::validate|model validation]] will be cancelled.
* @see Model::onBeforeValidate
*/
public $isValid = true;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment