Updating Model::invalidFields, so returning false from beforeValidate() will abort both the validation and saving() of the record.

Tests added to check beforeSave, beforeValidate, and beforeDelete return values.
Fixes #257
This commit is contained in:
mark_story 2009-11-04 12:36:17 -05:00
parent 8c46cc49fb
commit e609875754
4 changed files with 103 additions and 3 deletions

View file

@ -2359,7 +2359,7 @@ class Model extends Overloadable {
) ||
$this->beforeValidate($options) === false
) {
return $this->validationErrors;
return false;
}
if (!isset($this->validate) || empty($this->validate)) {
@ -2793,7 +2793,7 @@ class Model extends Overloadable {
function afterDelete() {
}
/**
* Called during save operations, before validation. Please note that custom
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @return boolean True if validate operation should continue, false to abort

View file

@ -565,7 +565,22 @@ class ModelDeleteTest extends BaseModelTest {
));
$this->assertEqual($result['Monkey'], $expected);
}
/**
* test that beforeDelete returning false can abort deletion.
*
* @return void
**/
function testBeforeDeleteDeleteAbortion() {
$this->loadFixtures('Post');
$Model =& new CallbackPostTestModel();
$Model->beforeDeleteReturn = false;
$result = $Model->delete(1);
$this->assertFalse($result);
$exists = $Model->findById(1);
$this->assertTrue(is_array($exists));
}
}
?>

View file

@ -601,6 +601,40 @@ class ModelWriteTest extends BaseModelTest {
$result = $TestModel->validates();
$this->assertTrue($result);
}
/**
* test that beforeValidate returning false can abort saves.
*
* @return void
**/
function testBeforeValidateSaveAbortion() {
$Model =& new CallbackPostTestModel();
$Model->beforeValidateReturn = false;
$data = array(
'title' => 'new article',
'body' => 'this is some text.'
);
$Model->create();
$result = $Model->save($data);
$this->assertFalse($result);
}
/**
* test that beforeSave returning false can abort saves.
*
* @return void
**/
function testBeforeSaveSaveAbortion() {
$Model =& new CallbackPostTestModel();
$Model->beforeSaveReturn = false;
$data = array(
'title' => 'new article',
'body' => 'this is some text.'
);
$Model->create();
$result = $Model->save($data);
$this->assertFalse($result);
}
/**
* testValidates method
*

View file

@ -1749,7 +1749,58 @@ class AssociationTest2 extends CakeTestModel {
* @subpackage cake.tests.cases.libs.model
*/
class Callback extends CakeTestModel {
//
}
/**
* CallbackPostTestModel class
*
* @package cake
* @subpackage cake.tests.cases.libs.model
*/
class CallbackPostTestModel extends CakeTestModel {
var $useTable = 'posts';
/**
* variable to control return of beforeValidate
*
* @var string
*/
var $beforeValidateReturn = true;
/**
* variable to control return of beforeSave
*
* @var string
*/
var $beforeSaveReturn = true;
/**
* variable to control return of beforeDelete
*
* @var string
*/
var $beforeDeleteReturn = true;
/**
* beforeSave callback
*
* @return void
**/
function beforeSave($options) {
return $this->beforeSaveReturn;
}
/**
* beforeValidate callback
*
* @return void
**/
function beforeValidate($options) {
return $this->beforeValidateReturn;
}
/**
* beforeDelete callback
*
* @return void
**/
function beforeDelete($cascade = true) {
return $this->beforeDeleteReturn;
}
}
/**
* Uuid class