Adding missing afterValidate callback to behaviors, Fixes #3024

This commit is contained in:
Jose Lorenzo Rodriguez 2012-07-10 20:33:49 -04:30
parent 8fcb2a7653
commit 39715bcd89
3 changed files with 56 additions and 0 deletions

View file

@ -285,6 +285,7 @@ class BehaviorCollection extends ObjectCollection implements CakeEventListener {
'Model.beforeFind' => 'trigger',
'Model.afterFind' => 'trigger',
'Model.beforeValidate' => 'trigger',
'Model.afterValidate' => 'trigger',
'Model.beforeSave' => 'trigger',
'Model.afterSave' => 'trigger',
'Model.beforeDelete' => 'trigger',

View file

@ -146,6 +146,17 @@ class ModelBehavior extends Object {
return true;
}
/**
* afterValidate is called just after model data was validated, you can use this callback
* to perform any data cleanup or preparation if needed
*
* @param Model $model Model using this behavior
* @return mixed False will stop this event from being passed to other behaviors
*/
public function afterValidate(Model $model) {
return true;
}
/**
* beforeSave is called before a model is saved. Returning false from a beforeSave callback
* will abort the save operation.

View file

@ -194,6 +194,29 @@ class TestBehavior extends ModelBehavior {
}
}
/**
* afterValidate method
*
* @param Model $model
* @param bool $cascade
* @return void
*/
public function afterValidate(Model $model) {
$settings = $this->settings[$model->alias];
if (!isset($settings['afterValidate']) || $settings['afterValidate'] == 'off') {
return parent::afterValidate($model);
}
switch ($settings['afterValidate']) {
case 'on':
return false;
break;
case 'test':
$model->data = array('foo');
return true;
break;
}
}
/**
* beforeDelete method
*
@ -966,6 +989,27 @@ class BehaviorCollectionTest extends CakeTestCase {
$this->assertSame($Apple->whitelist, array('unknown', 'name'));
}
/**
* testBehaviorValidateAfterCallback method
*
* @return void
*/
public function testBehaviorValidateAfterCallback() {
$Apple = new Apple();
$Apple->Behaviors->attach('Test');
$this->assertSame($Apple->validates(), true);
$Apple->Behaviors->attach('Test', array('afterValidate' => 'on'));
$this->assertSame($Apple->validates(), true);
$this->assertSame($Apple->validationErrors, array());
$Apple->Behaviors->attach('Test', array('afterValidate' => 'test'));
$Apple->data = array('bar');
$Apple->validates();
$this->assertEquals(array('foo'), $Apple->data);
}
/**
* testBehaviorValidateMethods method
*