Browse Source

Remove forced isNew = false in Marshaller::merge() for validation

The previous code made impossible to patch new entities and haver thier
validation run correctly.
Jose Lorenzo Rodriguez 11 years ago
parent
commit
b4e4300796
2 changed files with 38 additions and 1 deletions
  1. 1 1
      src/ORM/Marshaller.php
  2. 37 0
      tests/TestCase/ORM/MarshallerTest.php

+ 1 - 1
src/ORM/Marshaller.php

@@ -322,7 +322,7 @@ class Marshaller {
 			$data = $data[$tableName];
 		}
 
-		$errors = $this->_validate($data, $options, false);
+		$errors = $this->_validate($data, $options, $entity->isNew());
 		$schema = $this->_table->schema();
 		$properties = [];
 		foreach ($data as $key => $value) {

+ 37 - 0
tests/TestCase/ORM/MarshallerTest.php

@@ -1653,4 +1653,41 @@ class MarshallerTest extends TestCase {
 		$this->assertEmpty($result->errors('thing'));
 	}
 
+/**
+ * Test merge with validation and create or update validation rules
+ *
+ * @return void
+ */
+	public function testMergeWithCreate() {
+		$data = [
+			'title' => 'My title',
+			'author_id' => 'foo',
+		];
+		$marshall = new Marshaller($this->articles);
+		$entity = new Entity([
+			'title' => 'Foo',
+			'body' => 'My Content',
+			'author_id' => 1
+		]);
+		$entity->accessible('*', true);
+		$entity->isNew(true);
+		$entity->clean();
+
+		$this->articles->validator()
+			->requirePresence('thing', 'update')
+			->add('author_id', 'numeric', ['rule' => 'numeric', 'on' => 'update']);
+
+		$expected = clone $entity;
+		$result = $marshall->merge($expected, $data, []);
+
+		$this->assertEmpty($result->errors('author_id'));
+		$this->assertEmpty($result->errors('thing'));
+
+		$entity->clean();
+		$entity->isNew(false);
+		$result = $marshall->merge($entity, $data, []);
+		$this->assertNotEmpty($result->errors('author_id'));
+		$this->assertNotEmpty($result->errors('thing'));
+	}
+
 }