diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 51c3ed494..cc1d80385 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2183,7 +2183,7 @@ class Model extends Object implements CakeEventListener { if ($options['validate'] === 'first') { $validates = $this->validateAssociated($data, $options); - if ((!$validates && $options['atomic']) || (!$options['atomic'] && in_array(false, $validates, true))) { + if ((!$validates && $options['atomic']) || (!$options['atomic'] && in_array(false, Hash::flatten($validates), true))) { return $validates; } $options['validate'] = false; diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index 31327e7de..d317f6ca9 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -4830,6 +4830,51 @@ class ModelWriteTest extends BaseModelTest { $this->assertEquals($expected, $result[6]['Attachment']); } +/** + * Test that validate = first, atomic = false works when associated records + * fail validation. + * + * @return void + */ + public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() { + $this->loadFixtures('Comment', 'Article', 'User'); + $Article = ClassRegistry::init('Article'); + $Article->Comment->validator()->add('comment', array( + array('rule' => 'notEmpty') + )); + + $data = array( + 'Article' => array( + 'user_id' => 1, + 'title' => 'Foo', + 'body' => 'text', + 'published' => 'N' + ), + 'Comment' => array( + array( + 'user_id' => 1, + 'comment' => '', + 'published' => 'N', + ) + ), + ); + + $Article->saveAssociated( + $data, + array('validate' => 'first', 'atomic' => false) + ); + + $result = $Article->validationErrors; + $expected = array( + 'Comment' => array( + array( + 'comment' => array( 'This field cannot be left blank' ) + ) + ) + ); + $this->assertEquals($expected, $result); + } + /** * testSaveMany method *