From 0df1e90eeddbf1f2002618b5e39825d0494e7343 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Mon, 18 Jun 2012 13:46:19 +0200 Subject: [PATCH] show all validation errors when saving/validating Associated and primary model fails validation as well, fixes #2925 --- lib/Cake/Model/Model.php | 2 + lib/Cake/Model/ModelValidator.php | 2 + .../Test/Case/Model/ModelValidationTest.php | 44 +++++++++++-- lib/Cake/Test/Case/Model/ModelWriteTest.php | 66 +++++++++++++++---- 4 files changed, 96 insertions(+), 18 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 923ac287cd1..6e745a3c2f9 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2281,6 +2281,8 @@ public function saveAssociated($data = null, $options = array()) { if (isset($validationErrors[$this->alias])) { $this->validationErrors = $validationErrors[$this->alias]; + unset($validationErrors[$this->alias]); + $this->validationErrors = array_merge($this->validationErrors, $validationErrors); } if (!$options['atomic']) { diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index e6f9deeaf2d..406f05d45c9 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -162,6 +162,8 @@ public function validateAssociated(&$data, $options = array()) { $model->validationErrors = $validationErrors; if (isset($validationErrors[$model->alias])) { $model->validationErrors = $validationErrors[$model->alias]; + unset($validationErrors[$model->alias]); + $model->validationErrors = array_merge($model->validationErrors, $validationErrors); } if (!$options['atomic']) { return $return; diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index e7a2f3c3d8d..1a65be44d61 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -1280,7 +1280,10 @@ public function testSaveAllDeepValidateOnly() { $expected = array( 'Comment' => array( 'Article' => array( - 'body' => array('This field cannot be left blank') + 'body' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) ) ) ); @@ -1304,7 +1307,13 @@ public function testSaveAllDeepValidateOnly() { $result = $TestModel->Comment->Attachment->validationErrors; $expected = array( 'Comment' => array( - 'comment' => array('This field cannot be left blank') + 'comment' => array('This field cannot be left blank'), + 'Article' => array( + 'body' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) + ) ) ); $this->assertSame($expected, $result); @@ -1325,11 +1334,30 @@ public function testSaveAllDeepValidateOnly() { $this->assertFalse($result); $result = $TestModel->Comment->Attachment->validationErrors; - $expected = array('attachment' => array('This field cannot be left blank')); + $expected = array( + 'attachment' => array('This field cannot be left blank'), + 'Comment' => array( + 'comment' => array('This field cannot be left blank'), + 'Article' => array( + 'body' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) + ) + ) + ); $this->assertSame($expected, $result); $result = $TestModel->Comment->validationErrors; - $expected = array('comment' => array('This field cannot be left blank')); + $expected = array( + 'comment' => array('This field cannot be left blank'), + 'Article' => array( + 'body' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) + ) + ); $this->assertSame($expected, $result); $expected = array( @@ -1598,8 +1626,10 @@ public function testValidateAssociated() { $model->Attachment->validate = array('attachment' => 'notEmpty'); $model->Attachment->bindModel(array('belongsTo' => array('Comment'))); $expected = array( - 'Comment' => array('comment' => array('This field cannot be left blank')), - 'Attachment' => array('attachment' => array('This field cannot be left blank')) + 'comment' => array('This field cannot be left blank'), + 'Attachment' => array( + 'attachment' => array('This field cannot be left blank') + ) ); $data = array( @@ -1610,7 +1640,7 @@ public function testValidateAssociated() { $this->assertFalse($result); $result = $model->validateAssociated($data); $this->assertFalse($result); - $this->assertEquals($expected['Comment'], $model->validationErrors); + $this->assertEquals($expected, $model->validationErrors); $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors); } diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index eb0215c183f..2a35d9cdd13 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -2953,10 +2953,12 @@ public function testSaveAllHasOneValidation() { ); $this->assertEquals(false, $result); $expected = array( - 'Comment' => array('comment' => array('This field cannot be left blank')), - 'Attachment' => array('attachment' => array('This field cannot be left blank')) + 'comment' => array('This field cannot be left blank'), + 'Attachment' => array( + 'attachment' => array('This field cannot be left blank') + ) ); - $this->assertEquals($expected['Comment'], $model->validationErrors); + $this->assertEquals($expected, $model->validationErrors); $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors); } @@ -3207,7 +3209,15 @@ public function testSaveAllDeepMany() { $expected = array( 0 => array( - 'body' => array('This field cannot be left blank') + 'body' => array('This field cannot be left blank'), + 'Comment' => array( + 0 => array( + 'comment' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) + ) + ) ), 1 => array( 'Comment' => array( @@ -3468,7 +3478,10 @@ public function testSaveAllDeepValidateOnly() { $expected = array( 'Comment' => array( 'Article' => array( - 'body' => array('This field cannot be left blank') + 'body' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) ) ) ); @@ -3488,7 +3501,13 @@ public function testSaveAllDeepValidateOnly() { $result = $TestModel->Comment->Attachment->validationErrors; $expected = array( 'Comment' => array( - 'comment' => array('This field cannot be left blank') + 'comment' => array('This field cannot be left blank'), + 'Article' => array( + 'body' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) + ) ) ); $this->assertSame($expected, $result); @@ -3505,11 +3524,30 @@ public function testSaveAllDeepValidateOnly() { $this->assertFalse($result); $result = $TestModel->Comment->Attachment->validationErrors; - $expected = array('attachment' => array('This field cannot be left blank')); + $expected = array( + 'attachment' => array('This field cannot be left blank'), + 'Comment' => array( + 'comment' => array('This field cannot be left blank'), + 'Article' => array( + 'body' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) + ) + ) + ); $this->assertSame($expected, $result); $result = $TestModel->Comment->validationErrors; - $expected = array('comment' => array('This field cannot be left blank')); + $expected = array( + 'comment' => array('This field cannot be left blank'), + 'Article' => array( + 'body' => array('This field cannot be left blank'), + 'User' => array( + 'user' => array('This field cannot be left blank') + ) + ) + ); $this->assertSame($expected, $result); $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true)); @@ -5040,10 +5078,16 @@ public function testSaveAssociatedHasOneValidation() { ); $this->assertFalse($result); $expected = array( - 'Comment' => array('comment' => array('This field cannot be left blank')), - 'Attachment' => array('attachment' => array('This field cannot be left blank')) + 'comment' => array( + 'This field cannot be left blank' + ), + 'Attachment' => array( + 'attachment' => array( + 'This field cannot be left blank' + ) + ) ); - $this->assertEquals($expected['Comment'], $model->validationErrors); + $this->assertEquals($expected, $model->validationErrors); $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors); }