Skip to content
This repository
Browse code

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
Mark Story authored October 20, 2012
2  lib/Cake/Model/Model.php
@@ -2183,7 +2183,7 @@ public function saveAssociated($data = null, $options = array()) {
2183 2183
 
2184 2184
 		if ($options['validate'] === 'first') {
2185 2185
 			$validates = $this->validateAssociated($data, $options);
2186  
-			if ((!$validates && $options['atomic']) || (!$options['atomic'] && in_array(false, $validates, true))) {
  2186
+			if ((!$validates && $options['atomic']) || (!$options['atomic'] && in_array(false, Hash::flatten($validates), true))) {
2187 2187
 				return $validates;
2188 2188
 			}
2189 2189
 			$options['validate'] = false;
45  lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -4831,6 +4831,51 @@ public function testSaveAssociated() {
4831 4831
 	}
4832 4832
 
4833 4833
 /**
  4834
+ * Test that validate = first, atomic = false works when associated records
  4835
+ * fail validation.
  4836
+ *
  4837
+ * @return void
  4838
+ */
  4839
+	public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
  4840
+		$this->loadFixtures('Comment', 'Article', 'User');
  4841
+		$Article = ClassRegistry::init('Article');
  4842
+		$Article->Comment->validator()->add('comment', array(
  4843
+			array('rule' => 'notEmpty')
  4844
+		));
  4845
+
  4846
+		$data = array(
  4847
+			'Article' => array(
  4848
+				'user_id' => 1,
  4849
+				'title' => 'Foo',
  4850
+				'body' => 'text',
  4851
+				'published' => 'N'
  4852
+			),
  4853
+			'Comment' => array(
  4854
+				array(
  4855
+					'user_id' => 1,
  4856
+					'comment' => '',
  4857
+					'published' => 'N',
  4858
+				)
  4859
+			),
  4860
+		);
  4861
+
  4862
+		$Article->saveAssociated(
  4863
+			$data,
  4864
+			array('validate' => 'first', 'atomic' => false)
  4865
+		);
  4866
+
  4867
+		$result = $Article->validationErrors;
  4868
+		$expected = array(
  4869
+			'Comment' => array(
  4870
+				array(
  4871
+					'comment' => array( 'This field cannot be left blank' )
  4872
+				)
  4873
+			)
  4874
+		);
  4875
+		$this->assertEquals($expected, $result);
  4876
+	}
  4877
+
  4878
+/**
4834 4879
  * testSaveMany method
4835 4880
  *
4836 4881
  * @return void

0 notes on commit 08556ab

Please sign in to comment.
Something went wrong with that request. Please try again.