Skip to content

Commit

Permalink
Implemented the before and after validate events
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent dce5292 commit 17be14c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Cake/ORM/Table.php
Expand Up @@ -1057,11 +1057,28 @@ protected function _processValidation($entity, $options) {
$type = is_string($options['validate']) ? $options['validate'] : 'default';
$validator = $this->validator($type);

$pass = compact('entity', 'options', 'validator');
$event = new Event('Model.beforeValidate', $this, $pass);
$this->getEventManager()->dispatch($event);

if ($event->isStopped()) {
return (bool)$event->result;
}

if (!count($validator)) {
return true;
}

return $entity->validate($validator, $options['fieldList']);
$success = $entity->validate($validator, $options['fieldList']);

$event = new Event('Model.afterValidate', $this, $pass);
$this->getEventManager()->dispatch($event);

if ($event->isStopped()) {
$success = (bool)$event->result;
}

return $success;
}

/**
Expand Down
64 changes: 64 additions & 0 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -1903,4 +1903,68 @@ public function testSaveWithValidationSuccess() {
$this->assertEmpty($entity->errors('password'));
}

/**
* Tests beforeValidate event is triggered
*
* @return void
*/
public function testBeforeValidate() {
$entity = new \Cake\ORM\Entity([
'username' => 'superuser'
]);
$table = TableRegistry::get('users');
$table->getEventManager()->attach(function($ev, $en, $opt, $val) use ($entity) {
$this->assertSame($entity, $en);
$this->assertTrue($opt['crazy']);
$this->assertSame($ev->subject()->validator('default'), $val);
$val->validatePresence('password');
}, 'Model.beforeValidate');
$this->assertFalse($table->save($entity, ['crazy' => true]));
$this->assertNotEmpty($entity->errors('password'));
}

/**
* Tests that beforeValidate can set the validation result
*
* @return void
*/
public function testBeforeValidateSetResult() {
$entity = new \Cake\ORM\Entity([
'username' => 'superuser'
]);
$table = TableRegistry::get('users');
$table->getEventManager()->attach(function($ev, $en) {
$en->errors('username', 'Not good');
return false;
}, 'Model.beforeValidate');
$this->assertFalse($table->save($entity));
$this->assertEquals(['Not good'], $entity->errors('username'));
}

/**
* Tests that afterValidate is triggered and can set a result
*
* @return void
*/
public function testAfterValidate() {
$entity = new \Cake\ORM\Entity([
'username' => 'superuser',
'password' => 'hey'
]);
$table = TableRegistry::get('users');
$table->validator()->validatePresence('password');
$table->getEventManager()->attach(function($ev, $en, $opt, $val) use ($entity) {
$this->assertSame($entity, $en);
$this->assertTrue($opt['crazy']);
$this->assertSame($ev->subject()->validator('default'), $val);

$en->errors('username', 'Not good');
return false;
}, 'Model.afterValidate');

$this->assertFalse($table->save($entity, ['crazy' => true]));
$this->assertEmpty($entity->errors('password'));
$this->assertEquals(['Not good'], $entity->errors('username'));
}

}

0 comments on commit 17be14c

Please sign in to comment.