Skip to content
This repository
Browse code

Adding missing afterValidate callback to behaviors, Fixes #3024

  • Loading branch information...
commit 39715bcd89239dc5aaf219b99032ffe95a9bb26c 1 parent 8fcb2a7
José Lorenzo Rodríguez authored July 10, 2012
1  lib/Cake/Model/BehaviorCollection.php
@@ -285,6 +285,7 @@ public function implementedEvents() {
285 285
 			'Model.beforeFind' => 'trigger',
286 286
 			'Model.afterFind' => 'trigger',
287 287
 			'Model.beforeValidate' => 'trigger',
  288
+			'Model.afterValidate' => 'trigger',
288 289
 			'Model.beforeSave' => 'trigger',
289 290
 			'Model.afterSave' => 'trigger',
290 291
 			'Model.beforeDelete' => 'trigger',
11  lib/Cake/Model/ModelBehavior.php
@@ -147,6 +147,17 @@ public function beforeValidate(Model $model) {
147 147
 	}
148 148
 
149 149
 /**
  150
+ * afterValidate is called just after model data was validated, you can use this callback
  151
+ * to perform any data cleanup or preparation if needed
  152
+ *
  153
+ * @param Model $model Model using this behavior
  154
+ * @return mixed False will stop this event from being passed to other behaviors
  155
+ */
  156
+	public function afterValidate(Model $model) {
  157
+		return true;
  158
+	}
  159
+
  160
+/**
150 161
  * beforeSave is called before a model is saved.  Returning false from a beforeSave callback
151 162
  * will abort the save operation.
152 163
  *
44  lib/Cake/Test/Case/Model/BehaviorCollectionTest.php
@@ -195,6 +195,29 @@ public function beforeValidate(Model $model) {
195 195
 	}
196 196
 
197 197
 /**
  198
+ * afterValidate method
  199
+ *
  200
+ * @param Model $model
  201
+ * @param bool $cascade
  202
+ * @return void
  203
+ */
  204
+	public function afterValidate(Model $model) {
  205
+		$settings = $this->settings[$model->alias];
  206
+		if (!isset($settings['afterValidate']) || $settings['afterValidate'] == 'off') {
  207
+			return parent::afterValidate($model);
  208
+		}
  209
+		switch ($settings['afterValidate']) {
  210
+			case 'on':
  211
+				return false;
  212
+			break;
  213
+			case 'test':
  214
+				$model->data = array('foo');
  215
+				return true;
  216
+			break;
  217
+		}
  218
+	}
  219
+
  220
+/**
198 221
  * beforeDelete method
199 222
  *
200 223
  * @param Model $model
@@ -967,6 +990,27 @@ public function testBehaviorValidateCallback() {
967 990
 	}
968 991
 
969 992
 /**
  993
+ * testBehaviorValidateAfterCallback method
  994
+ *
  995
+ * @return void
  996
+ */
  997
+	public function testBehaviorValidateAfterCallback() {
  998
+		$Apple = new Apple();
  999
+
  1000
+		$Apple->Behaviors->attach('Test');
  1001
+		$this->assertSame($Apple->validates(), true);
  1002
+
  1003
+		$Apple->Behaviors->attach('Test', array('afterValidate' => 'on'));
  1004
+		$this->assertSame($Apple->validates(), true);
  1005
+		$this->assertSame($Apple->validationErrors, array());
  1006
+
  1007
+		$Apple->Behaviors->attach('Test', array('afterValidate' => 'test'));
  1008
+		$Apple->data = array('bar');
  1009
+		$Apple->validates();
  1010
+		$this->assertEquals(array('foo'), $Apple->data);
  1011
+	}
  1012
+
  1013
+/**
970 1014
  * testBehaviorValidateMethods method
971 1015
  *
972 1016
  * @return void

0 notes on commit 39715bc

Please sign in to comment.
Something went wrong with that request. Please try again.