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:
nate 2007-09-20 15:16:25 +00:00
parent f749683a8c
commit 09e64d075f
2 changed files with 31 additions and 1 deletions

View file

@ -64,6 +64,8 @@ class ModelBehavior extends Object {
function afterFind(&$model, $results, $primary) { }
function beforeValidate(&$model) { }
function beforeSave(&$model) { }
function afterSave(&$model, $created) { }

View file

@ -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;
}