Oihana PHP System

arrayMustHaveKeys.php

Table of Contents

Functions

arrayMustHaveKeys()  : string
Generates the 'array_must_have_keys:value1,value2,...' rule expression.

Functions

arrayMustHaveKeys()

Generates the 'array_must_have_keys:value1,value2,...' rule expression.

arrayMustHaveKeys(string ...$values) : string

The array must contain all the specified keys to be valid. This is useful to ensure that a nested array meets a prescribed format. The same thing can be achieved by using individual rules for each key with required. Note that this will still allow additional keys to be present, it merely validates the presence of specific keys.

This rule is best used in conjunction with the array rule, though it can be used standalone.

use Somnambulist\Components\Validation\Factory;

$validation = $factory->validate
([
    'filters' => ['foo' => 'bar', 'baz' => 'example']
],
[
    'filters' => 'array|array_must_have_keys:foo,bar,baz',
]);

$validation->passes(); // true if filters has all the keys in array_must_have_keys

The following examples are functionally equivalent:

use Somnambulist\Components\Validation\Factory;

$validation = $factory->validate
([
    'filters' => ['foo' => 'bar', 'baz' => 'example']
],
[
    'filters'     => 'array|array_must_have_keys:foo,bar,baz',
    'filters.foo' => 'string|between:1,50',
    'filters.bar' => 'numeric|min:1',
    'filters.baz' => 'uuid',
]);

$validation = $factory->validate
([
'    filters' => ['foo' => 'bar', 'baz' => 'example']
],
[
    'filters'     => 'array',
    'filters.foo' => 'required|string|between:1,50',
    'filters.bar' => 'required|numeric|min:1',
    'filters.baz' => 'required|uuid',
]);
Parameters
$values : string
Return values
string

        
On this page

Search results