Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
fccd8185
Commit
fccd8185
authored
Sep 05, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enhanced the default implementation of Model::scenarios() to return all scenarios found in rules().
parent
42a9d13b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
13 deletions
+34
-13
Model.php
framework/yii/base/Model.php
+34
-13
No files found.
framework/yii/base/Model.php
View file @
fccd8185
...
...
@@ -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
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment