Browse Source

Enhanced the default implementation of Model::scenarios() to return all scenarios found in rules().

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
fccd8185b5
  1. 47
      framework/yii/base/Model.php

47
framework/yii/base/Model.php

@ -45,7 +45,7 @@ use yii\validators\Validator;
* property is read-only. * property is read-only.
* @property ArrayIterator $iterator An iterator for traversing the items in the list. This property is * @property ArrayIterator $iterator An iterator for traversing the items in the list. This property is
* read-only. * read-only.
* @property string $scenario The scenario that this model is in. Defaults to 'default'. * @property string $scenario The scenario that this model is in. Defaults to [[DEFAULT_SCENARIO]].
* @property ArrayObject $validators All the validators declared in the model. This property is read-only. * @property ArrayObject $validators All the validators declared in the model. This property is read-only.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
@ -54,6 +54,11 @@ use yii\validators\Validator;
class Model extends Component implements IteratorAggregate, ArrayAccess class Model extends Component implements IteratorAggregate, ArrayAccess
{ {
/** /**
* The name of the default scenario.
*/
const DEFAULT_SCENARIO = 'default';
/**
* @event ModelEvent an event raised at the beginning of [[validate()]]. You may set * @event ModelEvent an event raised at the beginning of [[validate()]]. You may set
* [[ModelEvent::isValid]] to be false to stop the validation. * [[ModelEvent::isValid]] to be false to stop the validation.
*/ */
@ -74,7 +79,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
/** /**
* @var string current scenario * @var string current scenario
*/ */
private $_scenario = 'default'; private $_scenario = self::DEFAULT_SCENARIO;
/** /**
* Returns the validation rules for attributes. * Returns the validation rules for attributes.
@ -159,23 +164,39 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
* If an attribute should NOT be massively assigned (thus considered unsafe), * If an attribute should NOT be massively assigned (thus considered unsafe),
* please prefix the attribute with an exclamation character (e.g. '!rank'). * please prefix the attribute with an exclamation character (e.g. '!rank').
* *
* The default implementation of this method will return a 'default' scenario * The default implementation of this method will all scenarios found in the [[rules()]]
* which corresponds to all attributes listed in the validation rules applicable * declaration. A special scenario named [[DEFAULT_SCENARIO]] will contain all attributes
* to the 'default' scenario. * found in the [[rules()]]. Each scenario will be associated with the attributes that
* are being validated by the validation rules that apply to the scenario.
* *
* @return array a list of scenarios and the corresponding active attributes. * @return array a list of scenarios and the corresponding active attributes.
*/ */
public function scenarios() public function scenarios()
{ {
$attributes = array(); $scenarios = array();
foreach ($this->getActiveValidators() as $validator) { $defaults = array();
foreach ($validator->attributes as $name) { /** @var $validator Validator */
$attributes[$name] = true; foreach ($this->getValidators() as $validator) {
if (empty($validator->on)) {
foreach ($validator->attributes as $attribute) {
$defaults[$attribute] = true;
}
} else {
foreach ($validator->on as $scenario) {
foreach ($validator->attributes as $attribute) {
$scenarios[$scenario][$attribute] = true;
}
}
}
}
foreach ($scenarios as $scenario => $attributes) {
foreach (array_keys($defaults) as $attribute) {
$attributes[$attribute] = true;
} }
$scenarios[$scenario] = array_keys($attributes);
} }
return array( $scenarios[self::DEFAULT_SCENARIO] = array_keys($defaults);
'default' => array_keys($attributes), return $scenarios;
);
} }
/** /**
@ -593,7 +614,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
* Scenario affects how validation is performed and which attributes can * Scenario affects how validation is performed and which attributes can
* be massively assigned. * be massively assigned.
* *
* @return string the scenario that this model is in. Defaults to 'default'. * @return string the scenario that this model is in. Defaults to [[DEFAULT_SCENARIO]].
*/ */
public function getScenario() public function getScenario()
{ {

Loading…
Cancel
Save