Skip to content

Commit

Permalink
Added the Model.buildValidation event to tje tables
Browse files Browse the repository at this point in the history
This covers most of the cases for which beforeValidate was being used.
  • Loading branch information
lorenzo committed Dec 5, 2014
1 parent abfe59f commit 07cc015
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ORM/Table.php
Expand Up @@ -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)`
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -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');
}

}

0 comments on commit 07cc015

Please sign in to comment.