Skip to content

Commit 35c51bb

Browse files
committed
fix grammar
add exception throw on bad format
1 parent 219f22a commit 35c51bb

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Validation/Validator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ public function remove($field, $rule = null)
428428
* - `mode` individual mode for field
429429
* - `message` individual error message for field
430430
*
431-
* You can also set mode and message for all passed fields, the individual settings
432-
* takes precedence over group setting.
431+
* You can also set mode and message for all passed fields, the individual setting
432+
* takes precedence over group settings.
433433
*
434434
* @param string|array $field the name of the field
435435
* @param bool|string|callable $mode Valid values are true, false, 'create', 'update'.
@@ -450,10 +450,13 @@ public function requirePresence($field, $mode = true, $message = null)
450450
}
451451

452452
foreach ($field as $fieldName => $setting) {
453-
if (is_string($setting)) {
453+
if (is_string($setting) && is_int($fieldName)) {
454454
$fieldName = $setting;
455455
$setting = [];
456456
}
457+
if (!is_array($setting)) {
458+
throw new InvalidArgumentException(sprintf('Invalid field "%s" setting, must be an array.', $fieldName));
459+
}
457460
$setting += [
458461
'mode' => $mode,
459462
'message' => $message

tests/TestCase/Validation/ValidatorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ public function testRequirePresenceAsArray()
217217
$this->assertTrue($validator->field('subject')->isPresenceRequired());
218218
}
219219

220+
/**
221+
* Tests the requirePresence failure case
222+
*
223+
* @expectedException InvalidArgumentException
224+
* @return void
225+
*/
226+
public function testRequirePresenceAsArrayFailure()
227+
{
228+
$validator = new Validator();
229+
$validator->requirePresence(['title' => 'derp', 'created' => false]);
230+
}
231+
220232
/**
221233
* Tests the requirePresence method when passing a callback
222234
*

0 commit comments

Comments
 (0)