mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Implementing beforeValidate() behavior callback (Ticket #2818), and allowing $actsAs to be specified in AppModel
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5676 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
f749683a8c
commit
09e64d075f
2 changed files with 31 additions and 1 deletions
|
@ -64,6 +64,8 @@ class ModelBehavior extends Object {
|
|||
|
||||
function afterFind(&$model, $results, $primary) { }
|
||||
|
||||
function beforeValidate(&$model) { }
|
||||
|
||||
function beforeSave(&$model) { }
|
||||
|
||||
function afterSave(&$model, $created) { }
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
* Included libs
|
||||
*/
|
||||
uses('class_registry', 'validation', 'overloadable', 'model' . DS . 'behavior', 'model' . DS . 'connection_manager', 'set');
|
||||
|
||||
/**
|
||||
* Object-relational mapper.
|
||||
*
|
||||
|
@ -380,6 +381,22 @@ class Model extends Overloadable {
|
|||
}
|
||||
}
|
||||
|
||||
if (is_subclass_of($this, 'AppModel')) {
|
||||
$appVars = get_class_vars('AppModel');
|
||||
$actsAs = $appVars['actsAs'];
|
||||
$merge = array('actsAs');
|
||||
|
||||
if ($this->actsAs !== null || $this->actsAs !== false) {
|
||||
$merge[] = 'actsAs';
|
||||
}
|
||||
|
||||
foreach ($merge as $var) {
|
||||
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
|
||||
$this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->actsAs !== null && empty($this->behaviors)) {
|
||||
$callbacks = array('setup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave', 'beforeDelete', 'afterDelete', 'afterError');
|
||||
$this->actsAs = Set::normalize($this->actsAs);
|
||||
|
@ -1654,9 +1671,20 @@ class Model extends Overloadable {
|
|||
* Returns an array of invalid fields.
|
||||
*
|
||||
* @param array $data
|
||||
* @return array Array of invalid fields or boolean case any error occurs
|
||||
* @return array Array of invalid fields
|
||||
*/
|
||||
function invalidFields($data = array()) {
|
||||
|
||||
if (!empty($this->behaviors)) {
|
||||
$behaviors = array_keys($this->behaviors);
|
||||
$ct = count($behaviors);
|
||||
for ($i = 0; $i < $ct; $i++) {
|
||||
if ($this->behaviors[$behaviors[$i]]->beforeValidate($this) === false) {
|
||||
return $this->validationErrors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->beforeValidate()) {
|
||||
return $this->validationErrors;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue