Skip to content
Permalink
Browse files

allow requirePresence to pass array to validate multiple fields

  • Loading branch information...
Graziel committed Apr 6, 2016
1 parent 14af52a commit 1c2a9d9c8362c3002b5517664c8ce56cd41a02e0
Showing with 81 additions and 4 deletions.
  1. +24 −4 src/Validation/Validator.php
  2. +57 −0 tests/TestCase/Validation/ValidatorTest.php
@@ -424,7 +424,7 @@ public function remove($field, $rule = null)
/**
* Sets whether a field is required to be present in data array.
*
* @param string $field the name of the field
* @param string|array $field the name of the field
* @param bool|string|callable $mode Valid values are true, false, 'create', 'update'.
* If a callable is passed then the field will be required only when the callback
* returns true.
@@ -433,10 +433,30 @@ public function remove($field, $rule = null)
*/
public function requirePresence($field, $mode = true, $message = null)
{
$this->field($field)->isPresenceRequired($mode);
if ($message) {
$this->_presenceMessages[$field] = $message;
if (!is_array($field)) {
$field = [
$field => [
'mode' => $mode,
'message' => $message
]
];
}
foreach ($field as $fieldName => $setting) {
if (is_string($setting)) {
$fieldName = $setting;
$setting = [];
}
$setting += [
'mode' => $mode,
'message' => $message
];
$this->field($fieldName)->isPresenceRequired($setting['mode']);
if ($setting['message']) {
$this->_presenceMessages[$fieldName] = $setting['message'];
}
}
return $this;
}
@@ -191,6 +191,32 @@ public function testRequirePresence()
$this->assertEquals('update', $validator->field('title')->isPresenceRequired());
}
/**
* Tests the requirePresence method
*
* @return void
*/
public function testRequirePresenceAsArray()
{
$validator = new Validator;
$validator->requirePresence(['title', 'created']);
$this->assertTrue($validator->field('title')->isPresenceRequired());
$this->assertTrue($validator->field('created')->isPresenceRequired());
$validator->requirePresence([
'title' => [
'mode' => false
],
'content' => [
'mode' => 'update'
],
'subject'
], true);
$this->assertFalse($validator->field('title')->isPresenceRequired());
$this->assertEquals('update', $validator->field('content')->isPresenceRequired());
$this->assertTrue($validator->field('subject')->isPresenceRequired());
}
/**
* Tests the requirePresence method when passing a callback
*
@@ -383,6 +409,36 @@ public function testCustomErrorsWithPresenceRequired()
$this->assertEquals($expected, $errors);
}
/**
* Tests custom error messages generated when a field presence is required
*
* @return void
*/
public function testCustomErrorsWithPresenceRequiredAsArray()
{
$validator = new Validator;
$validator->requirePresence(['title', 'content'], true, 'Custom message');
$errors = $validator->errors(['foo' => 'something']);
$expected = [
'title' => ['_required' => 'Custom message'],
'content' => ['_required' => 'Custom message']
];
$this->assertEquals($expected, $errors);
$validator->requirePresence([
'title' => [
'message' => 'Test message'
],
'content'
], true, 'Custom message');
$errors = $validator->errors(['foo' => 'something']);
$expected = [
'title' => ['_required' => 'Test message'],
'content' => ['_required' => 'Custom message']
];
$this->assertEquals($expected, $errors);
}
/**
* Tests the allowEmpty method
*
@@ -1380,6 +1436,7 @@ public function testRangeFailure()
$validator = new Validator();
$validator->range('username', [1]);
}
/**
* Tests the url proxy method
*

0 comments on commit 1c2a9d9

Please sign in to comment.
You can’t perform that action at this time.