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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
5f8a714e
Commit
5f8a714e
authored
Jul 21, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
w
parent
7b7f159d
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
67 deletions
+41
-67
Component.php
framework/base/Component.php
+1
-1
Model.php
framework/base/Model.php
+40
-37
ModelEvent.php
framework/base/ModelEvent.php
+0
-29
No files found.
framework/base/Component.php
View file @
5f8a714e
...
@@ -364,7 +364,7 @@ class Component
...
@@ -364,7 +364,7 @@ class Component
* @param string $name the event name
* @param string $name the event name
* @return boolean whether there is any handler attached to the event.
* @return boolean whether there is any handler attached to the event.
*/
*/
public
function
hasEventHandler
(
$name
)
public
function
hasEventHandler
s
(
$name
)
{
{
$name
=
strtolower
(
$name
);
$name
=
strtolower
(
$name
);
return
isset
(
$this
->
_e
[
$name
])
&&
$this
->
_e
[
$name
]
->
getCount
();
return
isset
(
$this
->
_e
[
$name
])
&&
$this
->
_e
[
$name
]
->
getCount
();
...
...
framework/base/Model.php
View file @
5f8a714e
...
@@ -205,7 +205,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -205,7 +205,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
$this
->
clearErrors
();
$this
->
clearErrors
();
}
}
if
(
$this
->
beforeValidate
())
{
if
(
$this
->
beforeValidate
())
{
foreach
(
$this
->
getValidators
()
as
$validator
)
{
foreach
(
$this
->
get
Active
Validators
()
as
$validator
)
{
$validator
->
validate
(
$this
,
$attributes
);
$validator
->
validate
(
$this
,
$attributes
);
}
}
$this
->
afterValidate
();
$this
->
afterValidate
();
...
@@ -222,7 +222,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -222,7 +222,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/
*/
public
function
afterConstruct
()
public
function
afterConstruct
()
{
{
if
(
$this
->
hasEventHandler
(
'onAfterConstruct'
))
{
if
(
$this
->
hasEventHandler
s
(
'onAfterConstruct'
))
{
$this
->
onAfterConstruct
(
new
Event
(
$this
));
$this
->
onAfterConstruct
(
new
Event
(
$this
));
}
}
}
}
...
@@ -237,7 +237,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -237,7 +237,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/
*/
public
function
beforeValidate
()
public
function
beforeValidate
()
{
{
if
(
$this
->
hasEventHandler
(
'onBeforeValidate'
))
{
if
(
$this
->
hasEventHandler
s
(
'onBeforeValidate'
))
{
$event
=
new
ValidationEvent
(
$this
);
$event
=
new
ValidationEvent
(
$this
);
$this
->
onBeforeValidate
(
$event
);
$this
->
onBeforeValidate
(
$event
);
return
$event
->
isValid
;
return
$event
->
isValid
;
...
@@ -253,7 +253,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -253,7 +253,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/
*/
public
function
afterValidate
()
public
function
afterValidate
()
{
{
if
(
$this
->
hasEventHandler
(
'onAfterValidate'
))
{
if
(
$this
->
hasEventHandler
s
(
'onAfterValidate'
))
{
$this
->
onAfterValidate
(
new
CEvent
(
$this
));
$this
->
onAfterValidate
(
new
CEvent
(
$this
));
}
}
}
}
...
@@ -286,63 +286,66 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -286,63 +286,66 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
}
}
/**
/**
* Returns all the validators declared in the model.
* Returns all the validators declared in [[rules]].
* This method differs from {@link getValidators} in that the latter
*
* would only return the validators applicable to the current {@link scenario}.
* This method differs from [[getActiveValidators]] in that the latter
* Also, since this method return a {@link CList} object, you may
* only returns the validators applicable to the current [[scenario]].
* manipulate it by inserting or removing validators (useful in behaviors).
*
* For example, <code>$model->validatorList->add($newValidator)</code>.
* Because this method returns a [[Vector]] object, you may
* The change made to the {@link CList} object will persist and reflect
* manipulate it by inserting or removing validators (useful in model behaviors).
* in the result of the next call of {@link getValidators}.
* For example,
* @return CList all the validators declared in the model.
*
* ~~~php
* $model->validators->add($newValidator);
* ~~~
*
* @return Vector all the validators declared in the model.
*/
*/
public
function
getValidator
List
()
public
function
getValidator
s
()
{
{
if
(
$this
->
_validators
===
null
)
if
(
$this
->
_validators
===
null
)
{
$this
->
_validators
=
$this
->
createValidators
();
$this
->
_validators
=
$this
->
createValidators
();
}
return
$this
->
_validators
;
return
$this
->
_validators
;
}
}
/**
/**
* Returns the validators applicable to the current
{@link scenario}
.
* Returns the validators applicable to the current
[[scenario]]
.
* @param string $attribute the name of the attribute whose validators should be returned.
* @param string $attribute the name of the attribute whose
applicable
validators should be returned.
* If this is null, the validators for ALL attributes in the model will be returned.
* If this is null, the validators for ALL attributes in the model will be returned.
* @return array the validators applicable to the current
{@link scenario}
.
* @return array the validators applicable to the current
[[scenario]]
.
*/
*/
public
function
getValidators
(
$attribute
=
null
)
public
function
get
Active
Validators
(
$attribute
=
null
)
{
{
if
(
$this
->
_validators
===
null
)
$this
->
_validators
=
$this
->
createValidators
();
$validators
=
array
();
$validators
=
array
();
$scenario
=
$this
->
getScenario
();
$scenario
=
$this
->
getScenario
();
foreach
(
$this
->
_validators
as
$validator
)
foreach
(
$this
->
getValidators
()
as
$validator
)
{
{
if
(
$validator
->
applyTo
(
$scenario
))
{
if
(
$validator
->
applyTo
(
$scenario
))
if
(
$attribute
===
null
||
in_array
(
$attribute
,
$validator
->
attributes
,
true
))
{
{
if
(
$attribute
===
null
||
in_array
(
$attribute
,
$validator
->
attributes
,
true
))
$validators
[]
=
$validator
;
$validators
[]
=
$validator
;
}
}
}
}
}
return
$validators
;
return
$validators
;
}
}
/**
/**
* Creates validator objects based on the specification in {@link rules}.
* Creates validator objects based on the specification in {@link rules}.
* This method is mainly used internally.
* This method is mainly used internally.
* @return
CList
validators built based on {@link rules()}.
* @return
Vector
validators built based on {@link rules()}.
*/
*/
public
function
createValidators
()
public
function
createValidators
()
{
{
$validators
=
new
CList
;
$validators
=
new
Vector
;
foreach
(
$this
->
rules
()
as
$rule
)
foreach
(
$this
->
rules
()
as
$rule
)
{
{
if
(
isset
(
$rule
[
0
],
$rule
[
1
]))
{
// attributes, validator type
if
(
isset
(
$rule
[
0
],
$rule
[
1
]))
// attributes, validator name
$validators
->
add
(
\yii\validators\Validator
::
createValidator
(
$rule
[
1
],
$this
,
$rule
[
0
],
array_slice
(
$rule
,
2
)));
$validators
->
add
(
CValidator
::
createValidator
(
$rule
[
1
],
$this
,
$rule
[
0
],
array_slice
(
$rule
,
2
)));
}
else
else
{
throw
new
CException
(
Yii
::
t
(
'yii'
,
'{class} has an invalid validation rule. The rule must specify attributes to be validated and the validator name.'
,
throw
new
Exception
(
spr
'{class} has an invalid validation rule. The rule must specify attributes to be validated and the validator name.'
,
array
(
'{class}'
=>
get_class
(
$this
))));
array
(
'{class}'
=>
get_class
(
$this
))));
}
}
}
return
$validators
;
return
$validators
;
}
}
...
@@ -355,7 +358,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -355,7 +358,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/
*/
public
function
isAttributeRequired
(
$attribute
)
public
function
isAttributeRequired
(
$attribute
)
{
{
foreach
(
$this
->
getValidators
(
$attribute
)
as
$validator
)
foreach
(
$this
->
get
Active
Validators
(
$attribute
)
as
$validator
)
{
{
if
(
$validator
instanceof
CRequiredValidator
)
if
(
$validator
instanceof
CRequiredValidator
)
return
true
;
return
true
;
...
@@ -623,7 +626,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -623,7 +626,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
{
{
$attributes
=
array
();
$attributes
=
array
();
$unsafe
=
array
();
$unsafe
=
array
();
foreach
(
$this
->
getValidators
()
as
$validator
)
{
foreach
(
$this
->
get
Active
Validators
()
as
$validator
)
{
if
(
!
$validator
->
safe
)
{
if
(
!
$validator
->
safe
)
{
foreach
(
$validator
->
attributes
as
$name
)
{
foreach
(
$validator
->
attributes
as
$name
)
{
$unsafe
[]
=
$name
;
$unsafe
[]
=
$name
;
...
...
framework/base/ModelEvent.php
deleted
100644 → 0
View file @
7b7f159d
<?php
/**
* CModelEvent class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\base
;
/**
* ModelEvent class.
*
* ModelEvent represents the event parameters needed by events raised by a model.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ModelEvent
extends
Event
{
/**
* @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.
* This property could be null.
* @since 1.1.5
*/
public
$criteria
;
}
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