Skip to content

Commit

Permalink
show all validation errors when saving/validating Associated and prim…
Browse files Browse the repository at this point in the history
…ary model fails validation as well, fixes cakephp#2925
  • Loading branch information
ceeram committed Jun 18, 2012
1 parent 7ef83b8 commit 0df1e90
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 18 deletions.
2 changes: 2 additions & 0 deletions lib/Cake/Model/Model.php
Expand Up @@ -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']) {
Expand Down
2 changes: 2 additions & 0 deletions lib/Cake/Model/ModelValidator.php
Expand Up @@ -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;
Expand Down
44 changes: 37 additions & 7 deletions lib/Cake/Test/Case/Model/ModelValidationTest.php
Expand Up @@ -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')
)
)
)
);
Expand All @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}

Expand Down
66 changes: 55 additions & 11 deletions lib/Cake/Test/Case/Model/ModelWriteTest.php
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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')
)
)
)
);
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0df1e90

Please sign in to comment.