diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index c46f81160..73c1050ac 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2339,7 +2339,7 @@ class Model extends Object implements CakeEventListener { } } - $validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, $saved, true)))); + $validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true)))); if (!$validates) { $validationErrors[$key] = $this->validationErrors; } @@ -2480,7 +2480,7 @@ class Model extends Object implements CakeEventListener { } else { $saved = $Model->save($values, array('atomic' => false) + $options); } - $validates = ($saved === true || (is_array($saved) && !in_array(false, $saved, true))); + $validates = ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true))); } if ($validates) { @@ -2538,7 +2538,7 @@ class Model extends Object implements CakeEventListener { } } - $validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, $saved, true)))); + $validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true)))); if (!$validates) { $validationErrors[$association] = $Model->validationErrors; } diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index c0349ed48..b63a1828c 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -7684,6 +7684,128 @@ class ModelWriteTest extends BaseModelTest { $this->assertEquals(2, count($result['Attachment'])); } +/** + * testSaveManyDeepHasManyValidationFailure method + * + * @return void + */ + public function testSaveManyDeepHasManyValidationFailure() { + $this->loadFixtures('Article', 'Comment'); + $TestModel = new Article(); + $TestModel->Comment->validate = array( + 'comment' => array( + 'notEmpty' => array( + 'rule' => array('notEmpty'), + ) + ) + ); + + $result = $TestModel->saveMany(array( + array( + 'user_id' => 1, + 'title' => 'New Article', + 'body' => 'This article contains a invalid comment', + 'Comment' => array( + array( + 'user_id' => 1, + 'comment' => '' + ) + ) + ) + ), array('deep' => true)); + $this->assertFalse($result); + $this->assertEquals(array( + array( + 'Comment' => array( + array('comment' => array('notEmpty')) + ) + ) + ), $TestModel->validationErrors); + } + +/** + * testSaveAssociatedDeepHasOneHasManyValidateTrueValidationFailure method + * + * @return void + */ + public function testSaveAssociatedDeepHasOneHasManyValidateTrueValidationFailure() { + $this->loadFixtures('User', 'Article', 'Comment'); + $TestModel = new UserHasOneArticle(); + $TestModel->Article->Comment->validate = array( + 'comment' => array( + 'notEmpty' => array( + 'rule' => array('notEmpty'), + ) + ) + ); + + $result = $TestModel->saveAssociated(array( + 'User' => array( + 'user' => 'hiromi', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + ), + 'Article' => array( + 'title' => 'Article with User', + 'body' => 'This article will be saved with an user and contains a invalid comment', + 'Comment' => array( + array( + 'user_id' => 1, + 'comment' => '' + ) + ) + ) + ), array('deep' => true, 'validate' => true)); + $this->assertFalse($result); + $this->assertEquals(array( + 'Article' => array( + 'Comment' => array( + array('comment' => array('notEmpty')) + ) + ) + ), $TestModel->validationErrors); + } + +/** + * testSaveAssociatedDeepBelongsToHasManyValidateTrueValidationFailure method + * + * @return void + */ + public function testSaveAssociatedDeepBelongsToHasManyValidateTrueValidationFailure() { + $this->loadFixtures('ArticlesTag', 'Article', 'Comment'); + $TestModel = new ArticlesTagBelongsToArticle(); + $TestModel->Article->Comment->validate = array( + 'comment' => array( + 'notEmpty' => array( + 'rule' => array('notEmpty'), + ) + ) + ); + + $result = $TestModel->saveAssociated(array( + 'ArticlesTagBelongsToArticle' => array( + 'tag_id' => 1, + ), + 'Article' => array( + 'title' => 'Article with User', + 'body' => 'This article will be saved with an user and contains a invalid comment', + 'Comment' => array( + array( + 'user_id' => 1, + 'comment' => '' + ) + ) + ) + ), array('deep' => true, 'validate' => true)); + $this->assertFalse($result); + $this->assertEquals(array( + 'Article' => array( + 'Comment' => array( + array('comment' => array('notEmpty')) + ) + ) + ), $TestModel->validationErrors); + } + /** * testUpdateAllBoolean * diff --git a/lib/Cake/Test/Case/Model/models.php b/lib/Cake/Test/Case/Model/models.php index ced7a56b3..ad7781115 100644 --- a/lib/Cake/Test/Case/Model/models.php +++ b/lib/Cake/Test/Case/Model/models.php @@ -5079,3 +5079,50 @@ class Example extends AppModel { ); } + +/** + * UserHasOneArticle class + * + * @package Cake.Test.Case.Model + */ +class UserHasOneArticle extends AppModel { + +/** + * useTable property + * + * @var string + */ + public $useTable = 'users'; + +/** + * hasOne property + * + * @var array + */ + public $hasOne = array('Article'); + +} + + +/** + * ArticlesTagBelongsToArticle class + * + * @package Cake.Test.Case.Model + */ +class ArticlesTagBelongsToArticle extends CakeTestModel { + +/** + * useTable property + * + * @var string + */ + public $useTable = 'articles_tags'; + +/** + * belongsTo property + * + * @var array + */ + public $belongsTo = array('Article'); + +}