Browse Source

Merge pull request #3884 from cakephp/3.0-validation-fixes

3.0 validation fixes
Mark Story 11 years ago
parent
commit
2d5244ce2d
2 changed files with 27 additions and 2 deletions
  1. 3 2
      src/ORM/Table.php
  2. 24 0
      tests/TestCase/ORM/TableTest.php

+ 3 - 2
src/ORM/Table.php

@@ -1136,8 +1136,9 @@ class Table implements RepositoryInterface, EventListener {
 
 		$associated = $options['associated'];
 		$options['associated'] = [];
+		$validate = $options['validate'];
 
-		if ($options['validate'] && !$this->validate($entity, $options)) {
+		if ($validate && !$this->validate($entity, $options)) {
 			return false;
 		}
 
@@ -1153,7 +1154,7 @@ class Table implements RepositoryInterface, EventListener {
 			$this,
 			$entity,
 			$options['associated'],
-			$options->getArrayCopy()
+			['validate' => (bool)$validate] + $options->getArrayCopy()
 		);
 
 		if (!$saved && $options['atomic']) {

+ 24 - 0
tests/TestCase/ORM/TableTest.php

@@ -2084,6 +2084,30 @@ class TableTest extends \Cake\TestSuite\TestCase {
 	}
 
 /**
+ * Tests using a custom validation object when saving and saving associations
+ *
+ * @return void
+ */
+	public function testSaveWithDifferentValidatorAndAssociations() {
+		$entity = new \Cake\ORM\Entity([
+			'title' => 'foo',
+			'body' => 'bar',
+			'author' => new \Cake\ORM\Entity([
+				'name' => 'Susan'
+			])
+		]);
+		$table = TableRegistry::get('articles');
+		$table->belongsTo('authors');
+		$validator = (new Validator)->validatePresence('body');
+		$table->validator('custom', $validator);
+
+		$validator2 = (new Validator)->validatePresence('thing');
+		$table->authors->validator('default', $validator2);
+		$this->assertFalse($table->save($entity, ['validate' => 'custom']), 'default was not used');
+		$this->assertNotEmpty($entity->author->errors('thing'));
+	}
+
+/**
  * Test magic findByXX method.
  *
  * @return void