Browse Source

fix grammar
add exception throw on bad format

Paweł Kukiełka 10 years ago
parent
commit
35c51bb5a5
2 changed files with 18 additions and 3 deletions
  1. 6 3
      src/Validation/Validator.php
  2. 12 0
      tests/TestCase/Validation/ValidatorTest.php

+ 6 - 3
src/Validation/Validator.php

@@ -428,8 +428,8 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      * - `mode` individual mode for field
      * - `message` individual error message for field
      *
-     * You can also set mode and message for all passed fields, the individual settings
-     * takes precedence over group setting.
+     * You can also set mode and message for all passed fields, the individual setting
+     * takes precedence over group settings.
      *
      * @param string|array $field the name of the field
      * @param bool|string|callable $mode Valid values are true, false, 'create', 'update'.
@@ -450,10 +450,13 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         }
 
         foreach ($field as $fieldName => $setting) {
-            if (is_string($setting)) {
+            if (is_string($setting) && is_int($fieldName)) {
                 $fieldName = $setting;
                 $setting = [];
             }
+            if (!is_array($setting)) {
+                throw new InvalidArgumentException(sprintf('Invalid field "%s" setting, must be an array.', $fieldName));
+            }
             $setting += [
                 'mode' => $mode,
                 'message' => $message

+ 12 - 0
tests/TestCase/Validation/ValidatorTest.php

@@ -218,6 +218,18 @@ class ValidatorTest extends TestCase
     }
 
     /**
+     * Tests the requirePresence failure case
+     *
+     * @expectedException InvalidArgumentException
+     * @return void
+     */
+    public function testRequirePresenceAsArrayFailure()
+    {
+        $validator = new Validator();
+        $validator->requirePresence(['title' => 'derp', 'created' => false]);
+    }
+
+    /**
      * Tests the requirePresence method when passing a callback
      *
      * @return void