Browse Source

Correctly setting the error message for existsIn() when an array is used

Jose Lorenzo Rodriguez 11 years ago
parent
commit
e7580d31ea
2 changed files with 22 additions and 1 deletions
  1. 1 1
      src/ORM/RulesChecker.php
  2. 21 0
      tests/TestCase/ORM/RulesCheckerIntegrationTest.php

+ 1 - 1
src/ORM/RulesChecker.php

@@ -354,7 +354,7 @@ class RulesChecker
             }
         }
 
-        $errorField = $field;
+        $errorField = is_string($field) ? $field : current($field);
         return $this->_addError(new ExistsIn($field, $table), '_existsIn', compact('errorField', 'message'));
     }
 

+ 21 - 0
tests/TestCase/ORM/RulesCheckerIntegrationTest.php

@@ -634,6 +634,27 @@ class RulesCheckerIntegrationTest extends TestCase
     }
 
     /**
+     * Tests that using an array in existsIn() sets the error message correctly
+     *
+     * @return
+     */
+    public function testExistsInErrorWithArrayField()
+    {
+        $entity = new Entity([
+            'title' => 'An Article',
+            'author_id' => 500
+        ]);
+
+        $table = TableRegistry::get('Articles');
+        $table->belongsTo('Authors');
+        $rules = $table->rulesChecker();
+        $rules->add($rules->existsIn(['author_id'], 'Authors'));
+
+        $this->assertFalse($table->save($entity));
+        $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->errors('author_id'));
+    }
+
+    /**
      * Tests using rules to prevent delete operations
      *
      * @group delete