Skip to content

Commit

Permalink
Implementend validator::atLeast and atMost
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 28, 2016
1 parent 96416bc commit 5098058
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Validation/Validator.php
Expand Up @@ -1319,6 +1319,56 @@ public function multipleOptions($field, array $options = [], $message = null, $w
]);
}

/**
* Add a validation rule to ensure that a field is an array containing at least
* the specified amount of elements
*
* @param string $field The field you want to apply the rule to.
* @param int $count The number of elements the array should at least have
* @param string|null $message The error message when the rule fails.
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
* true when the validation rule should be applied.
* @see \Cake\Validation\Validation::numElements()
* @return $this
*/
public function hasAtLeast($field, $count, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);
return $this->add($field, 'hasAtLeast', $extra + [
'rule' => function ($value) use ($count) {
if (is_array($value) && isset($value['_ids'])) {
$value = $value['_ids'];
}
return Validation::numElements($value, '>=', $count);
}
]);
}

/**
* Add a validation rule to ensure that a field is an array containing at most
* the specified amount of elements
*
* @param string $field The field you want to apply the rule to.
* @param int $count The number maximim amount of elements the field should have
* @param string|null $message The error message when the rule fails.
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
* true when the validation rule should be applied.
* @see \Cake\Validation\Validation::numElements()
* @return $this
*/
public function hasAtMost($field, $count, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);
return $this->add($field, 'hasAtMost', $extra + [
'rule' => function ($value) use ($count) {
if (is_array($value) && isset($value['_ids'])) {
$value = $value['_ids'];
}
return Validation::numElements($value, '<=', $count);
}
]);
}

/**
* Returns whether or not a field can be left empty for a new or already existing
* record.
Expand Down
40 changes: 40 additions & 0 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -1545,6 +1545,46 @@ public function testMultiple()
$this->assertNotEmpty($validator->errors(['username' => '']));
}

/**
* Tests the hasAtLeast method
*
* @return void
*/
public function testHasAtLeast()
{
$validator = new Validator();
$validator->hasAtLeast('things', 3);
$this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
$this->assertEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
$this->assertNotEmpty($validator->errors(['things' => [1, 2]]));
$this->assertNotEmpty($validator->errors(['things' => []]));
$this->assertNotEmpty($validator->errors(['things' => 'string']));

$this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
$this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
$this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
$this->assertNotEmpty($validator->errors(['things' => ['_ids' => []]]));
$this->assertNotEmpty($validator->errors(['things' => ['_ids' => 'string']]));
}

/**
* Tests the hasAtMost method
*
* @return void
*/
public function testHasAtMost()
{
$validator = new Validator();
$validator->hasAtMost('things', 3);
$this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
$this->assertEmpty($validator->errors(['things' => [1]]));
$this->assertNotEmpty($validator->errors(['things' => [1, 2, 3, 4]]));

$this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
$this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
$this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
}

protected function assertProxyMethod($validator, $method, $extra = null, $pass = [], $name = null)
{
$name = $name ?: $method;
Expand Down

0 comments on commit 5098058

Please sign in to comment.