Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 08556ab879769ba0184e4df89c1d4778eabcd97c 1 parent 888b1f4
@markstory markstory authored
View
2  lib/Cake/Model/Model.php
@@ -2183,7 +2183,7 @@ public function saveAssociated($data = null, $options = array()) {
if ($options['validate'] === 'first') {
$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;
}
$options['validate'] = false;
View
45 lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -4831,6 +4831,51 @@ public function testSaveAssociated() {
}
/**
+ * 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
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.