Permalink
Browse files

Adding missing afterValidate callback to behaviors, Fixes #3024

  • Loading branch information...
lorenzo committed Jul 11, 2012
1 parent 8fcb2a7 commit 39715bcd89239dc5aaf219b99032ffe95a9bb26c
@@ -285,6 +285,7 @@ public function implementedEvents() {
'Model.beforeFind' => 'trigger',
'Model.afterFind' => 'trigger',
'Model.beforeValidate' => 'trigger',
+ 'Model.afterValidate' => 'trigger',
'Model.beforeSave' => 'trigger',
'Model.afterSave' => 'trigger',
'Model.beforeDelete' => 'trigger',
@@ -146,6 +146,17 @@ public function beforeValidate(Model $model) {
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.
@@ -194,6 +194,29 @@ public function beforeValidate(Model $model) {
}
}
+/**
+ * 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 @@ public function testBehaviorValidateCallback() {
$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
*

0 comments on commit 39715bc

Please sign in to comment.