From 08556ab879769ba0184e4df89c1d4778eabcd97c Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 20 Oct 2012 15:12:05 -0400 Subject: [PATCH] Fix saveAssociated() with validate=first, atomic=false When using the above options & validation errors on the associated models, saving would not be aborted. Fixes #3285 --- lib/Cake/Model/Model.php | 2 +- lib/Cake/Test/Case/Model/ModelWriteTest.php | 45 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) 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 *