From 07cc015184f7d38928c4a69ba4efb2801e9f9a4d Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 5 Dec 2014 15:01:51 +0100 Subject: [PATCH] Added the Model.buildValidation event to tje tables This covers most of the cases for which beforeValidate was being used. --- src/ORM/Table.php | 6 +++++- tests/TestCase/ORM/TableTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ORM/Table.php b/src/ORM/Table.php index d92ab0bcb9a..eb1086e5774 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -83,7 +83,10 @@ * $primary parameter indicates whether or not this is the root query, * or an associated query. * - * - `buildRules(Event $event, RulesChecker $rules)` + * - `buildValidator(Event $event, Validator $validator, string $name)` + * Allows listeners to modify validation rules for the provided named validator. + * + * - `buildRules(Event $event, RulesChecker $rules)` * Allows listeners to modify the rules checker by adding more rules. * * - `beforeRules(Event $event, Entity $entity, RulesChecker $rules)` @@ -1064,6 +1067,7 @@ public function validator($name = 'default', Validator $validator = null) { if ($validator === null) { $validator = new Validator(); $validator = $this->{'validation' . ucfirst($name)}($validator); + $this->dispatchEvent('Model.buildValidator', compact('validator', 'name')); } $validator->provider('table', $this); diff --git a/tests/TestCase/ORM/TableTest.php b/tests/TestCase/ORM/TableTest.php index 9a35f8c83e7..9f9d27ddf8e 100644 --- a/tests/TestCase/ORM/TableTest.php +++ b/tests/TestCase/ORM/TableTest.php @@ -3059,4 +3059,23 @@ public function testInitializeEvent() { EventManager::instance()->detach($cb, 'Model.initialize'); } +/** + * Tests that calling validator() trigger the buildValidator event + * + * @return void + */ + public function testBuildValidatorEvent() { + $count = 0; + $cb = function ($event) use (&$count){ + $count++; + }; + EventManager::instance()->attach($cb, 'Model.buildValidator'); + $articles = TableRegistry::get('Articles'); + $articles->validator(); + $this->assertEquals(1, $count, 'Callback should be called'); + + $articles->validator(); + $this->assertEquals(1, $count, 'Callback should be called only once'); + } + }