Browse Source

Add beforeValidate, afterValidate callbacks for behaviors.

ADmad 11 years ago
parent
commit
5db7bb41e7
2 changed files with 33 additions and 2 deletions
  1. 9 0
      src/ORM/Behavior.php
  2. 24 2
      tests/TestCase/ORM/BehaviorTest.php

+ 9 - 0
src/ORM/Behavior.php

@@ -53,6 +53,13 @@ use Cake\Event\EventListener;
  *   $primary parameter indicates whether or not this is the root query,
  *   or an associated query.
  *
+ * - `beforeValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
+ *   Fired before an entity is validated. By stopping this event, you can abort
+ *   the validate + save operations.
+ *
+ * - `afterValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
+ *   Fired after an entity is validated.
+ *
  * - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
  *   Fired before each entity is saved. Stopping this event will abort the save
  *   operation. When the event is stopped the result of the event will be returned.
@@ -211,6 +218,8 @@ class Behavior implements EventListener {
 			'Model.afterSave' => 'afterSave',
 			'Model.beforeDelete' => 'beforeDelete',
 			'Model.afterDelete' => 'afterDelete',
+			'Model.beforeValidate' => 'beforeValidate',
+			'Model.afterValidate' => 'afterValidate',
 		];
 		$config = $this->config();
 		$priority = isset($config['priority']) ? $config['priority'] : null;

+ 24 - 2
tests/TestCase/ORM/BehaviorTest.php

@@ -28,6 +28,18 @@ class TestBehavior extends Behavior {
 	public function beforeFind() {
 	}
 
+/**
+ * Test for event bindings.
+ */
+	public function beforeValidate() {
+	}
+
+/**
+ * Test for event bindings.
+ */
+	public function afterValidate() {
+	}
+
 }
 
 /**
@@ -171,7 +183,9 @@ class BehaviorTest extends TestCase {
 		$table = $this->getMock('Cake\ORM\Table');
 		$behavior = new TestBehavior($table);
 		$expected = [
-			'Model.beforeFind' => 'beforeFind'
+			'Model.beforeFind' => 'beforeFind',
+			'Model.beforeValidate' => 'beforeValidate',
+			'Model.afterValidate' => 'afterValidate',
 		];
 		$this->assertEquals($expected, $behavior->implementedEvents());
 	}
@@ -188,7 +202,15 @@ class BehaviorTest extends TestCase {
 			'Model.beforeFind' => [
 				'priority' => 10,
 				'callable' => 'beforeFind'
-			]
+			],
+			'Model.beforeValidate' => [
+				'priority' => 10,
+				'callable' => 'beforeValidate'
+			],
+			'Model.afterValidate' => [
+				'priority' => 10,
+				'callable' => 'afterValidate'
+			],
 		];
 		$this->assertEquals($expected, $behavior->implementedEvents());
 	}