diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 1e014fe64..0a4e91acb 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1310,9 +1310,11 @@ class Model extends Overloadable { $validationErrors[$this->id] = $this->validationErrors; } } + $validating = ($options['validate'] === 'only' || $options['validate'] === 'first'); + if (!$options['atomic']) { $return[] = $validates; - } elseif (!$validates) { + } elseif (!$validates && !$validating) { break; } } @@ -1367,8 +1369,10 @@ class Model extends Overloadable { if (!$options['atomic']) { $return[$this->alias][] = $validates; } + $validating = ($options['validate'] === 'only' || $options['validate'] === 'first'); + foreach ($data as $association => $values) { - if (!$validates) { + if (!$validates && !$validating) { break; } if (isset($associations[$association])) { diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 843ada5a1..e9e125c48 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -2182,6 +2182,41 @@ class ModelTest extends CakeTestCase { $this->assertEqual($result, $expected); } + function testSaveAllHasOneValidation() { + $model = new Comment(); + $model->deleteAll(true); + $this->assertEqual($model->find('all'), array()); + + $model->Attachment->deleteAll(true); + $this->assertEqual($model->Attachment->find('all'), array()); + + $model->validate = array('comment' => VALID_NOT_EMPTY); + $model->Attachment->validate = array('attachment' => VALID_NOT_EMPTY); + $model->Attachment->bind('Comment'); + + $this->assertFalse($model->saveAll( + array( + 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1), + 'Attachment' => array('attachment' => '') + ), + array('validate' => 'first') + )); + $expected = array( + 'Comment' => array('comment' => 'This field cannot be left blank'), + 'Attachment' => array('attachment' => 'This field cannot be left blank') + ); + $this->assertEqual($model->validationErrors, $expected); + + $this->assertFalse($model->saveAll( + array( + 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1), + 'Attachment' => array('attachment' => '') + ), + array('validate' => 'only') + )); + $this->assertEqual($model->validationErrors, $expected); + } + function testSaveAllAtomic() { $this->loadFixtures('Article', 'User'); $TestModel =& new Article();