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 ArrayIterator $iterator An iterator for traversing the items in the list. This property is
* 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.
*
* @author Qiang Xue <qiang.xue@gmail.com>
@ -54,6 +54,11 @@ use yii\validators\Validator;
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
* [[ModelEvent::isValid]] to be false to stop the validation.
*/
@ -74,7 +79,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
/**
* @var string current scenario
*/
private $_scenario = 'default';
private $_scenario = self::DEFAULT_SCENARIO;
/**
* 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),
* please prefix the attribute with an exclamation character (e.g. '!rank').
*
* The default implementation of this method will return a 'default' scenario
* which corresponds to all attributes listed in the validation rules applicable
* to the 'default' scenario.
* The default implementation of this method will all scenarios found in the [[rules()]]
* declaration. A special scenario named [[DEFAULT_SCENARIO]] will contain all attributes
* 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.
*/
public function scenarios()
{
$attributes = array();
foreach ($this->getActiveValidators() as $validator) {
foreach ($validator->attributes as $name) {
$attributes[$name] = true;
$scenarios = array();
$defaults = array();
/** @var $validator Validator */
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(
'default' => array_keys($attributes),
);
$scenarios[self::DEFAULT_SCENARIO] = array_keys($defaults);
return $scenarios;
}
/**
@ -593,7 +614,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
* Scenario affects how validation is performed and which attributes can
* 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()
{

Loading…
Cancel
Save