Skip to content
Permalink
Browse files

Implemented the before and after validate events

  • Loading branch information...
lorenzo committed Nov 17, 2013
1 parent dce5292 commit 17be14ce212ac64f5a7650de273ac74f7ce0d3c7
Showing with 82 additions and 1 deletion.
  1. +18 −1 Cake/ORM/Table.php
  2. +64 −0 Cake/Test/TestCase/ORM/TableTest.php
@@ -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;
}
/**
@@ -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.
You can’t perform that action at this time.