show all validation errors when saving/validating Associated and primary model fails validation as well, fixes #2925

This commit is contained in:
Ceeram 2012-06-18 13:46:19 +02:00
parent 7ef83b89f5
commit 0df1e90eed
4 changed files with 96 additions and 18 deletions

View file

@ -2281,6 +2281,8 @@ class Model extends Object implements CakeEventListener {
if (isset($validationErrors[$this->alias])) {
$this->validationErrors = $validationErrors[$this->alias];
unset($validationErrors[$this->alias]);
$this->validationErrors = array_merge($this->validationErrors, $validationErrors);
}
if (!$options['atomic']) {

View file

@ -162,6 +162,8 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
$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;

View file

@ -1280,7 +1280,10 @@ class ModelValidationTest extends BaseModelTest {
$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 @@ class ModelValidationTest extends BaseModelTest {
$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 @@ class ModelValidationTest extends BaseModelTest {
$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 @@ class ModelValidationTest extends BaseModelTest {
$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 @@ class ModelValidationTest extends BaseModelTest {
$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);
}

View file

@ -2953,10 +2953,12 @@ class ModelWriteTest extends BaseModelTest {
);
$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 @@ class ModelWriteTest extends BaseModelTest {
$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 @@ class ModelWriteTest extends BaseModelTest {
$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 @@ class ModelWriteTest extends BaseModelTest {
$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 @@ class ModelWriteTest extends BaseModelTest {
$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 @@ class ModelWriteTest extends BaseModelTest {
);
$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);
}