Oihana PHP System

helpers

Table of Contents

Functions

after()  : string
Generates the 'after:date' rule expression.
arrayCanOnlyHaveKeys()  : string
Generates the 'array_can_only_have_keys:value1,value2,...' rule expression.
arrayMustHaveKeys()  : string
Generates the 'array_must_have_keys:value1,value2,...' rule expression.
before()  : string
Generates the 'before:date' rule expression.
between()  : string
Generates the 'between:min,max' rule expression.
date()  : string
Generates the 'date[:format]' rule expression.
defaultValue()  : string
Generates the 'default:value' rule expression.
different()  : string
Generates the 'different:anotherField' rule expression.
digits()  : string
Generates the 'digits:value' rule expression.
digitsBetween()  : string
Generates the 'digits_between:min,max' rule expression.
endsWith()  : string
Generates the 'ends_with:anotherField' rule expression.
extension()  : string
Generates the 'extension:extension_a,extension_b,...' rule expression.
in()  : string
Generates the 'in:value1,value2,...' rule expression.
length()  : string
Generates the 'length:number' rule expression.
max()  : string
Generates the 'max:value' rule expression.
mimes()  : string
Generates the 'mimes:extension_a,extension_b,...' rule expression.
min()  : string
Generates the 'min:value' rule expression.
notIn()  : string
Generates the 'not_in:value1,value2,...' rule expression.
prohibitedIf()  : string
Generates the 'prohibited_if:anotherField' rule expression.
prohibitedUnless()  : string
Generates the 'prohibited_unless:anotherField' rule expression.
regex()  : string
Generates the 'regex:/your-regex/' rule expression.
requiredIf()  : string
Generates the 'required_if:anotherField,value1,value2,...' rule expression.
requiredUnless()  : string
Generates the 'required_unless:anotherField,value1,value2,...' rule expression.
requiredWith()  : string
Generates the 'required_with:field1,field2,...' rule expression.
requiredWithAll()  : string
The field under validation must be present and not empty only if all the other specified fields are present.
requiredWithout()  : string
Generates the 'required_without:field1,field2,...' rule expression.
requiredWithoutAll()  : string
Generates the 'required_without_all:field1,field2,...' rule expression.
requires()  : string
Generates the 'requires:field1,field2,...' rule expression.
rule()  : string
Generates a 'rule_name[:value1,value2,...]' rule expression.
rules()  : string
Generates a concatenated validation rules string from multiple rules.
same()  : string
Generates the 'same:anotherField' rule expression.
startsWith()  : string
Generates the 'starts_with:anotherField' rule expression.
url()  : string
Generates the 'url[:scheme]' rule expression.

Functions

after()

Generates the 'after:date' rule expression.

after(string $date) : string

The parameter should be any valid string that can be parsed by strtotime.

For example:

  • after:next week
  • after:2016-12-31
  • after:2016
  • after:2016-12-31 09:56:02
Parameters
$date : string

The date format pattern.

Return values
string

arrayCanOnlyHaveKeys()

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

arrayCanOnlyHaveKeys(string ...$values) : string

The array can only contain the specified keys, any keys not present will fail validation. By default, associative data has no restrictions on the key => values that can be present. For example: you have filters for a search box that are passed to SQL, only the specified keys should be allowed to be sent and not any value in the array of filters.

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_can_only_have_keys:foo,bar',
]);

$validation->passes(); // true if filters only has the keys in array_can_only_have_keys
Parameters
$values : string
Return values
string

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

before()

Generates the 'before:date' rule expression.

before(string $date) : string

The field under this rule must be a date before the given maximum.

This also works the same way as the after rule. Pass anything that can be parsed by strtotime

Parameters
$date : string

The date format pattern.

Tags
see
after()
Return values
string

between()

Generates the 'between:min,max' rule expression.

between(string|int|float $min, string|int|float $max) : string

The field under this rule must have a size between min and max params. Value size is calculated in the same way as min and max rule.

You can also validate the maximum size of uploaded files using this rule:

$validation = $validator->validate
([
   'photo' => $_FILES['photo']
],
[
    'photo' => 'required|between:1M,2M'
]);
Parameters
$min : string|int|float
$max : string|int|float
Return values
string

date()

Generates the 'date[:format]' rule expression.

date([string|null $format = null ]) : string

The field under this rule must be valid date following a given format. Parameter format is optional, default format is Y-m-d.

Parameters
$format : string|null = null

The date format pattern (Default Y-m-d)

Return values
string

defaultValue()

Generates the 'default:value' rule expression.

defaultValue(mixed $value) : string

If the attribute has no value, this default will be used in place in the validated data.

For example if you have validation like this

Parameters
$value : mixed

The default value.

Return values
string

different()

Generates the 'different:anotherField' rule expression.

different(string $anotherField) : string

Opposite of same; the field value under this rule must be different to another_field value.

Parameters
$anotherField : string

The another field to evaluates.

Return values
string

digits()

Generates the 'digits:value' rule expression.

digits(int $value) : string

The field under validation must be numeric and must have an exact length of value.

Parameters
$value : int
Return values
string

digitsBetween()

Generates the 'digits_between:min,max' rule expression.

digitsBetween(int $min, int $max) : string

The field under validation must be numeric and have a length between the given min and max.

Parameters
$min : int
$max : int
Return values
string

endsWith()

Generates the 'ends_with:anotherField' rule expression.

endsWith(string $anotherField) : string

The field under this validation must end with another_field. Comparison can be against strings, numbers and array elements.

Parameters
$anotherField : string

The another field to evaluates.

Return values
string

extension()

Generates the 'extension:extension_a,extension_b,...' rule expression.

extension(string ...$values) : string

The field under this rule must end with an extension corresponding to one of those listed.

This is useful for validating a file type for a given path or url. The mimes rule should be used for validating uploads.

If you require strict mime checking you should implement a custom MimeTypeGuesser that can make use of a server side file checker that uses a mime library.

Parameters
$values : string
Return values
string

in()

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

in(string ...$values) : string

The field under this rule must be included in the given list of values.

To help build the string rule, the In (and NotIn) rules have a helper method:

use Somnambulist\Components\Validation\Factory;
use Somnambulist\Components\Validation\Rules\In;

$factory = new Factory();
$validation = $factory->validate( $data,
[
   'enabled' =>
   [
       'required',
       In::make([true, 1])
   ]
]);
Parameters
$values : string
Return values
string

length()

Generates the 'length:number' rule expression.

length(string|int $value) : string

The field under this validation must be a string of exactly the length specified.

Parameters
$value : string|int
Return values
string

max()

Generates the 'max:value' rule expression.

max(string|int|float $value) : string

The field under this rule must have a size less than or equal to the given number.

Value size is calculated in the same way as the min rule.

You can also validate the maximum size of uploaded files using this rule:

$validation = $validator->validate
([
   'photo' => $_FILES['photo']
],
[
   'photo' => 'required|max:2M'
]);
Parameters
$value : string|int|float
Return values
string

mimes()

Generates the 'mimes:extension_a,extension_b,...' rule expression.

mimes(string ...$values) : string

The $_FILES item under validation must have a MIME type corresponding to one of the listed extensions.

This works on file extension and not client sent headers or embedded file type.

If you require strict mime type validation you are recommended to implement a custom MimeTypeGuesser that uses a full mime-type lookup library and replace the built-in mime rule.

Parameters
$values : string
Return values
string

min()

Generates the 'min:value' rule expression.

min(string|int|float $value) : string

The field under this rule must have a size greater than or equal to the given number.

For string values, the size corresponds to the number of characters. For integer or float values, size corresponds to its numerical value. For an array, size corresponds to the count of the array.

If your value is numeric string, you can use the numeric rule to treat its size as a numeric value instead of the number of characters.

You can also validate the minimum size of uploaded files using this rule:

$validation = $validator->validate
([
    'photo' => $_FILES['photo']
],
[
    'photo' => 'required|min:1M'
]);
Parameters
$value : string|int|float
Return values
string

notIn()

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

notIn(string ...$values) : string

The field under this rule must not be included in the given list of values.

This rule also uses in_array and can have strict checks enabled the same way as In.

Parameters
$values : string
Return values
string

prohibitedIf()

Generates the 'prohibited_if:anotherField' rule expression.

prohibitedIf(string $anotherField, mixed ...$values) : string

The field under this rule is not allowed if another_field is provided with any of the value(s).

Parameters
$anotherField : string

The another field to evaluates.

$values : mixed
Return values
string

prohibitedUnless()

Generates the 'prohibited_unless:anotherField' rule expression.

prohibitedUnless(string $anotherField, mixed ...$values) : string

The field under this rule is not allowed unless another_field has one of these values. This is the inverse of prohibited_if.

Parameters
$anotherField : string

The another field to evaluates.

$values : mixed
Return values
string

regex()

Generates the 'regex:/your-regex/' rule expression.

regex(string $regex) : string

The field under this rule must match the given regex.

Note: if you require the use of |, then the regex rule must be written in array format instead of as a string. For example:

use Somnambulist\Components\Validation\Factory;

$validation = new Factory()->validate
(
   [ 'field' => 'value' ] ,
   [
      'field' =>
      [
         'required' ,
         'regex' => '/(this|that|value)/'
      ]
    ]
)
Parameters
$regex : string
Return values
string

requiredIf()

Generates the 'required_if:anotherField,value1,value2,...' rule expression.

requiredIf(string $anotherField, mixed ...$values) : string

The field under this rule must be present and not empty if the another_field field is equal to any value.

For example required_if:something,1,yes,on will be required if something's value is one of 1, '1', 'yes', or 'on'.

Parameters
$anotherField : string

The another field to evaluates.

$values : mixed
Return values
string

requiredUnless()

Generates the 'required_unless:anotherField,value1,value2,...' rule expression.

requiredUnless(string $anotherField, mixed ...$values) : string

The field under validation must be present and not empty unless the another_field field is equal to any value.

Parameters
$anotherField : string

The another field to evaluates.

$values : mixed
Return values
string

requiredWith()

Generates the 'required_with:field1,field2,...' rule expression.

requiredWith(string ...$fields) : string

The field under validation must be present and not empty only if any of the other specified fields are present.

Note: the behaviour of this rule can be circumvented if the rule this is defined on is sometimes or nullable.

For example: if a is "required_with:b", but a is also only sometimes present, then the required_with will never trigger as the sometimes rule will negate it. a would also need to be explicitly passed to trigger the rule.

Parameters
$fields : string
Return values
string

requiredWithAll()

The field under validation must be present and not empty only if all the other specified fields are present.

requiredWithAll(string ...$fields) : string
Parameters
$fields : string
Return values
string

requiredWithout()

Generates the 'required_without:field1,field2,...' rule expression.

requiredWithout(string ...$fields) : string

The field under validation must be present and not empty only when any of the other specified fields are not present.

Parameters
$fields : string
Return values
string

requiredWithoutAll()

Generates the 'required_without_all:field1,field2,...' rule expression.

requiredWithoutAll(string ...$fields) : string

The field under validation must be present and not empty only when all the other specified fields are not present.

Parameters
$fields : string
Return values
string

requires()

Generates the 'requires:field1,field2,...' rule expression.

requires(string ...$fields) : string

The field under validation requires that the specified fields are present in the input data and are not empty.

For example: field b "requires:a"; if a is either not present, or has an "empty" value, then the validation fails. "empty" is false, empty string, or null.

This is an extension of required_with, however the rule will fail when used with sometimes or nullable. For example: if b "requires:a" and "a" is allowed to be nullable, b will fail as it explicitly requires a with a value.

Parameters
$fields : string
Return values
string

rule()

Generates a 'rule_name[:value1,value2,...]' rule expression.

rule(string $name, mixed ...$values) : string
Parameters
$name : string

The name of the rule

$values : mixed

The optional values to passed-in.

Return values
string

rules()

Generates a concatenated validation rules string from multiple rules.

rules(string|array<string|int, mixed> ...$rules) : string

This helper allows you to pass a list of rules as strings or arrays and concatenates them using the pipe (|) character, which is commonly used in validation libraries (like Somnambulist Validation or Laravel) to separate multiple validation constraints.

Parameters
$rules : string|array<string|int, mixed>

One or more rules to combine.

Tags
example
use function oihana\validations\rules\helpers\rules;

// Pass rules as an array
$rules = rules([ 'required', 'min:5', 'max:10' ]);
// Returns: 'required|min:5|max:10'

// Pass rules as separate arguments
$rules = rules('required', 'min:5', 'max:10');
// Returns: 'required|min:5|max:10'

// Mixed usage
$rules = rules('required', ['min:5', 'max:10']);
// Returns: 'required|min:5|max:10'
see
compile()

Helper used internally to join rules.

Return values
string

same()

Generates the 'same:anotherField' rule expression.

same(string $anotherField) : string

The field value under this rule must have the same value as another_field.

Parameters
$anotherField : string

The another field to evaluates.

Return values
string

startsWith()

Generates the 'starts_with:anotherField' rule expression.

startsWith(string $anotherField) : string

The field under this validation must start with another_field. Comparison can be against strings, numbers and array elements.

Parameters
$anotherField : string

The another field to evaluates.

Return values
string

url()

Generates the 'url[:scheme]' rule expression.

url([null|array<string|int, mixed>|string $scheme = null ]) : string

The field under this rule must be a valid url format. The default is to validate the common format: any_scheme://.... You can specify specific URL schemes if you wish.

Example:

$validation = new Factory()->validate( $inputs ,
[
    'random_url' => 'url' ,          // value can be `any_scheme://...`
    'https_url' => 'url:http' ,      // value must be started with `https://`
    'http_url' => 'url:http,https' , // value must be started with `http://` or `https://`
    'ftp_url' => 'url:ftp' ,         // value must be started with `ftp://`
    'custom_url' => 'url:custom',   // value must be started with `custom://`
]);
Parameters
$scheme : null|array<string|int, mixed>|string = null

The scheme(s) of the url to evaluates.

Return values
string

        
On this page

Search results