Fixing issues with validation rules not being reset after calling

create() on the model or when calling validates() multiple times
This had interesting side effects when deep saving
This commit is contained in:
Jose Lorenzo Rodriguez 2012-08-25 13:29:16 +02:00
parent ccaa042f2e
commit 60eb228ddf
4 changed files with 25 additions and 6 deletions

View file

@ -282,6 +282,17 @@ class CakeValidationRule {
return true;
}
/**
* Resets interal state for this rule, by default it will become valid
* and it will set isUpdate() to false
*
* @return void
**/
public function reset() {
$this->_valid = true;
$this->_recordExists = false;
}
/**
* Returns passed options for this rule
*

View file

@ -117,6 +117,7 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
* @return array list of validation errors for this field
*/
public function validate($data, $isUpdate = false) {
$this->reset();
$errors = array();
foreach ($this->getRules() as $name => $rule) {
$rule->isUpdate($isUpdate);
@ -143,6 +144,17 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
return $errors;
}
/**
* Resets interal state for all validation rules in this set
*
* @return void
**/
public function reset() {
foreach ($this->getRules() as $rule) {
$rule->reset();
}
}
/**
* Gets a rule for a given name if exists
*

View file

@ -3242,7 +3242,6 @@ class ModelWriteTest extends BaseModelTest {
)
),
1 => array(
'body' => array('This field cannot be left blank'),
'Comment' => array(
0 => array(
'User' => array(
@ -3687,9 +3686,6 @@ class ModelWriteTest extends BaseModelTest {
$expected = array(
0 => array(
'body' => array('This field cannot be left blank')
),
1 => array(
'body' => array('This field cannot be left blank')
)
);
$result = $TestModel->validationErrors;
@ -3703,7 +3699,7 @@ class ModelWriteTest extends BaseModelTest {
)
),
array(
'Article' => array('id' => 2, 'body' => 'Same here'),
'Article' => array('id' => 2),
'Comment' => array(
array('comment' => '', 'published' => 'Y', 'user_id' => 2)
)

View file

@ -276,7 +276,7 @@ class Article extends CakeTestModel {
public $validate = array(
'user_id' => 'numeric',
'title' => array('required' => false, 'rule' => 'notEmpty'),
'body' => 'notEmpty',
'body' => array('required' => false, 'rule' => 'notEmpty'),
);
/**