Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding missing afterValidate callback to behaviors, Fixes #3024

  • Loading branch information...
commit 39715bcd89239dc5aaf219b99032ffe95a9bb26c 1 parent 8fcb2a7
@lorenzo lorenzo authored
View
1  lib/Cake/Model/BehaviorCollection.php
@@ -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',
View
11 lib/Cake/Model/ModelBehavior.php
@@ -147,6 +147,17 @@ public function beforeValidate(Model $model) {
}
/**
+ * 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
44 lib/Cake/Test/Case/Model/BehaviorCollectionTest.php
@@ -195,6 +195,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
*
* @param Model $model
@@ -967,6 +990,27 @@ public function testBehaviorValidateCallback() {
}
/**
+ * 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
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.