mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge pull request #280 from apapsch/master
Create arrayHasKeys validator.
This commit is contained in:
commit
7705cbc5a0
18
README.md
18
README.md
@ -167,6 +167,7 @@ V::lang('ar');
|
|||||||
* `creditCard` - Field is a valid credit card number
|
* `creditCard` - Field is a valid credit card number
|
||||||
* `instanceOf` - Field contains an instance of the given class
|
* `instanceOf` - Field contains an instance of the given class
|
||||||
* `optional` - Value does not need to be included in data array. If it is however, it must pass validation.
|
* `optional` - Value does not need to be included in data array. If it is however, it must pass validation.
|
||||||
|
* `arrayHasKeys` - Field is an array and contains all specified keys.
|
||||||
|
|
||||||
**NOTE**: If you are comparing floating-point numbers with min/max validators, you
|
**NOTE**: If you are comparing floating-point numbers with min/max validators, you
|
||||||
should install the [BCMath](http://us3.php.net/manual/en/book.bc.php)
|
should install the [BCMath](http://us3.php.net/manual/en/book.bc.php)
|
||||||
@ -938,6 +939,23 @@ $v->rules([
|
|||||||
$v->validate();
|
$v->validate();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## arrayHasKeys fields usage
|
||||||
|
|
||||||
|
The `arrayHasKeys` rule ensures that the field is an array and that it contains all the specified keys.
|
||||||
|
Returns false if the field is not an array or if no required keys are specified or if some key is missing.
|
||||||
|
|
||||||
|
```php
|
||||||
|
$v = new Valitron\Validator([
|
||||||
|
'address' => [
|
||||||
|
'name' => 'Jane Doe',
|
||||||
|
'street' => 'Doe Square',
|
||||||
|
'city' => 'Doe D.C.'
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$v->rule(['arrayHasKeys', 'address', ['name', 'street', 'city']);
|
||||||
|
$v->validate();
|
||||||
|
```
|
||||||
|
|
||||||
## Adding Custom Validation Rules
|
## Adding Custom Validation Rules
|
||||||
|
|
||||||
To add your own validation rule, use the `addRule` method with a rule
|
To add your own validation rule, use the `addRule` method with a rule
|
||||||
|
|||||||
@ -35,4 +35,5 @@ return array(
|
|||||||
'instanceOf' => "must be an instance of '%s'",
|
'instanceOf' => "must be an instance of '%s'",
|
||||||
'containsUnique' => "must contain unique elements only",
|
'containsUnique' => "must contain unique elements only",
|
||||||
'subset' => "contains an item that is not in the list",
|
'subset' => "contains an item that is not in the list",
|
||||||
|
'arrayHasKeys' => "does not contain all required keys",
|
||||||
);
|
);
|
||||||
|
|||||||
@ -911,6 +911,23 @@ class Validator
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function validateArrayHasKeys($field, $value, $params)
|
||||||
|
{
|
||||||
|
if (!is_array($value) || !isset($params[0])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$requiredFields = $params[0];
|
||||||
|
if (count($requiredFields) === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
foreach ($requiredFields as $fieldName) {
|
||||||
|
if (!array_key_exists($fieldName, $value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get array of fields and data
|
* Get array of fields and data
|
||||||
*
|
*
|
||||||
|
|||||||
@ -2483,6 +2483,72 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v->rule('date', 'data.*.foo');
|
$v->rule('date', 'data.*.foo');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testArrayHasKeysTrueIfAllFieldsExist()
|
||||||
|
{
|
||||||
|
$v = new Validator(array(
|
||||||
|
'address' => array(
|
||||||
|
'name' => 'Jane Doe',
|
||||||
|
'street' => 'Doe Square',
|
||||||
|
'city' => 'Doe D.C.'
|
||||||
|
)
|
||||||
|
));
|
||||||
|
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayHasKeysFalseOnMissingField()
|
||||||
|
{
|
||||||
|
$v = new Validator(array(
|
||||||
|
'address' => array(
|
||||||
|
'name' => 'Jane Doe',
|
||||||
|
'street' => 'Doe Square'
|
||||||
|
)
|
||||||
|
));
|
||||||
|
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayHasKeysFalseOnNonArray()
|
||||||
|
{
|
||||||
|
$v = new Validator(array(
|
||||||
|
'address' => 'Jane Doe, Doe Square'
|
||||||
|
));
|
||||||
|
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayHasKeysFalseOnEmptyRequiredFields()
|
||||||
|
{
|
||||||
|
$v = new Validator(array(
|
||||||
|
'address' => array(
|
||||||
|
'lat' => 77.547,
|
||||||
|
'lon' => 16.337
|
||||||
|
)
|
||||||
|
));
|
||||||
|
$v->rule('arrayHasKeys', 'address', array());
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayHasKeysFalseOnUnspecifiedRequiredFields()
|
||||||
|
{
|
||||||
|
$v = new Validator(array(
|
||||||
|
'address' => array(
|
||||||
|
'lat' => 77.547,
|
||||||
|
'lon' => 16.337
|
||||||
|
)
|
||||||
|
));
|
||||||
|
$v->rule('arrayHasKeys', 'address');
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayHasKeysTrueIfMissingAndOptional()
|
||||||
|
{
|
||||||
|
$v = new Validator(array());
|
||||||
|
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
|
||||||
|
$v->rule('optional', 'address');
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sampleFunctionCallback($field, $value, array $params)
|
function sampleFunctionCallback($field, $value, array $params)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user