From 35c51bb5a53f820964fbecf117566105d5eb3004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kukie=C5=82ka?= Date: Fri, 8 Apr 2016 11:16:57 +0200 Subject: [PATCH] fix grammar add exception throw on bad format --- src/Validation/Validator.php | 9 ++++++--- tests/TestCase/Validation/ValidatorTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Validation/Validator.php b/src/Validation/Validator.php index 9f672433f86..480648861fa 100644 --- a/src/Validation/Validator.php +++ b/src/Validation/Validator.php @@ -428,8 +428,8 @@ public function remove($field, $rule = null) * - `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 @@ public function requirePresence($field, $mode = true, $message = null) } 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 diff --git a/tests/TestCase/Validation/ValidatorTest.php b/tests/TestCase/Validation/ValidatorTest.php index 924a8f582b8..ba5ff1c1028 100644 --- a/tests/TestCase/Validation/ValidatorTest.php +++ b/tests/TestCase/Validation/ValidatorTest.php @@ -217,6 +217,18 @@ public function testRequirePresenceAsArray() $this->assertTrue($validator->field('subject')->isPresenceRequired()); } + /** + * 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 *