Skip to content

Commit

Permalink
fix grammar
Browse files Browse the repository at this point in the history
add exception throw on bad format
  • Loading branch information
Graziel committed Apr 8, 2016
1 parent 219f22a commit 35c51bb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Validation/Validator.php
Expand Up @@ -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'.
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 35c51bb

Please sign in to comment.