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
This commit is contained in:
mark_story 2012-10-20 15:12:05 -04:00
parent 888b1f4795
commit 08556ab879
2 changed files with 46 additions and 1 deletions

View file

@ -2183,7 +2183,7 @@ class Model extends Object implements CakeEventListener {
if ($options['validate'] === 'first') { if ($options['validate'] === 'first') {
$validates = $this->validateAssociated($data, $options); $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; return $validates;
} }
$options['validate'] = false; $options['validate'] = false;

View file

@ -4830,6 +4830,51 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals($expected, $result[6]['Attachment']); $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 * testSaveMany method
* *