Browse Source

Merge branch 'master' into 2.3

mark_story 13 years ago
parent
commit
ffcf71c810
2 changed files with 122 additions and 2 deletions
  1. 2 2
      lib/Cake/Model/ModelValidator.php
  2. 120 0
      lib/Cake/Test/Case/Model/ModelValidationTest.php

+ 2 - 2
lib/Cake/Model/ModelValidator.php

@@ -156,7 +156,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
 						$data[$association] = $model->{$association}->data[$model->{$association}->alias];
 					}
 					if (is_array($validates)) {
-						if (in_array(false, $validates, true)) {
+						if (in_array(false, Hash::flatten($validates), true)) {
 							$validates = false;
 						} else {
 							$validates = true;
@@ -219,7 +219,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
 				$validates = $model->set($record) && $model->validates($options);
 				$data[$key] = $model->data;
 			}
-			if ($validates === false || (is_array($validates) && in_array(false, $validates, true))) {
+			if ($validates === false || (is_array($validates) && in_array(false, Hash::flatten($validates), true))) {
 				$validationErrors[$key] = $model->validationErrors;
 				$validates = false;
 			} else {

+ 120 - 0
lib/Cake/Test/Case/Model/ModelValidationTest.php

@@ -2208,4 +2208,124 @@ class ModelValidationTest extends BaseModelTest {
 		$this->assertFalse($model->validates());
 	}
 
+/**
+ * Test validateAssociated with atomic=false & deep=true
+ *
+ * @return void
+ */
+	public function testValidateAssociatedAtomicFalseDeepTrueWithErrors() {
+		$this->loadFixtures('Comment', 'Article', 'User', 'Attachment');
+		$Attachment = ClassRegistry::init('Attachment');
+		$Attachment->Comment->validator()->add('comment', array(
+			array('rule' => 'notEmpty')
+		));
+		$Attachment->Comment->User->bindModel(array(
+			'hasMany' => array(
+				'Article',
+				'Comment'
+			)),
+			false
+		);
+
+		$data = array(
+			'Attachment' => array(
+				'attachment' => 'text',
+				'Comment' => array(
+					'comment' => '',
+					'published' => 'N',
+					'User' => array(
+						'user' => 'Foo',
+						'password' => 'mypassword',
+						'Comment' => array(
+							array(
+								'comment' => ''
+							)
+						)
+					)
+				)
+			)
+		);
+		$result = $Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
+
+		$result = $Attachment->validationErrors;
+		$expected = array(
+			'Comment' => array(
+				'comment' => array(
+					0 => 'This field cannot be left blank',
+				),
+				'User' => array(
+					'Comment' => array(
+						0 => array(
+							'comment' => array(
+								0 => 'This field cannot be left blank',
+							),
+						),
+					),
+				),
+			),
+		);
+		$this->assertEquals($result, $expected);
+	}
+
+/**
+ * Test validateMany with atomic=false & deep=true
+ *
+ * @return void
+ */
+	public function testValidateManyAtomicFalseDeepTrueWithErrors() {
+		$this->loadFixtures('Comment', 'Article', 'User');
+		$Article = ClassRegistry::init('Article');
+		$Article->Comment->validator()->add('comment', array(
+			array('rule' => 'notEmpty')
+		));
+
+		$data = array(
+			array(
+				'Article' => array(
+					'user_id' => 1,
+					'title' => 'Foo',
+					'body' => 'text',
+					'published' => 'N'
+				),
+				'Comment' => array(
+					array(
+						'user_id' => 1,
+						'comment' => 'Baz',
+						'published' => 'N',
+					)
+				),
+			),
+			array(
+				'Article' => array(
+					'user_id' => 1,
+					'title' => 'Bar',
+					'body' => 'text',
+					'published' => 'N'
+				),
+				'Comment' => array(
+					array(
+						'user_id' => 1,
+						'comment' => '',
+						'published' => 'N',
+					)
+				),
+			),
+		);
+		$Article->validateMany($data, array('atomic' => false, 'deep' => true));
+
+		$result = $Article->validationErrors;
+		$expected = array(
+			1 => array(
+				'Comment' => array(
+					0 => array(
+						'comment' => array(
+							0 => 'This field cannot be left blank',
+						),
+					),
+				),
+			),
+		);
+		$this->assertEquals($result, $expected);
+	}
+
 }