Oihana PHP

Rules uses ConstantsTrait

The available rules constants.

Tags
see
https://github.com/somnambulist-tech/validation?tab=readme-ov-file#available-rules
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

ACCEPTED  : string = 'accepted'
The field under this rule must be one of 'on', 'yes', '1', 'true' (the string "true"), or true.
AFTER  : string = 'after'
The field under this rule must be a date after the given minimum.
ALPHA  : string = 'alpha'
The field under this rule must be entirely alphabetic characters.
ALPHA_DASH  : string = 'alpha_dash'
The field under this rule may have alpha-numeric characters, as well as dashes and underscores.
ALPHA_NUM  : string = 'alpha_num'
The field under this rule must be entirely alpha-numeric characters.
ALPHA_SPACES  : string = 'alpha_spaces'
The field under this rule may have alpha characters, as well as spaces.
ANY_OF  : string = 'any'
A variation of in: here the values (separated by default with a ,) must all be in the given values.
ARRAY  : string = 'array'
The field under this rule must be an array.
ARRAY_CAN_ONLY_HAVE_KEYS  : string = 'array_can_only_have_keys'
The array can only contain the specified keys, any keys not present will fail validation.
ARRAY_MUST_HAVE_KEYS  : string = 'array_must_have_keys'
The array must contain all the specified keys to be valid.
BEFORE  : string = 'before'
The field under this rule must be a date before the given maximum.
BETWEEN  : string = 'between'
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.
BOOLEAN  : string = 'boolean'
The field under this rule must be boolean. Accepted inputs are true, false, 1, 0, "1", and "0".
CALLBACK  : string = 'callback'
Define a custom callback to validate the value. This rule cannot be registered using the string syntax.
COLOR  : string = 'color'
Ensures that a given value matches a valid color expression (e.g. "#ff0000").
DATE  : string = 'date'
The field under this rule must be valid date following a given format.
DEFAULT  : string = 'default'
If the attribute has no value, this default will be used in place in the validated data.
DIFFERENT  : string = 'different'
Opposite of same; the field value under this rule must be different to another_field value.
DIGITS  : string = 'digits'
The field under validation must be numeric and must have an exact length of value.
DIGITS_BETWEEN  : string = 'digits_between'
The field under validation must be numeric and have a length between the given min and max.
ELEVATION  : string = 'elevation'
Ensures that a value represents a valid elevation (altitude) in meters.
EMAIL  : string = 'email'
The field under this validation must be a valid email address according to the built-in PHP filter extension.
ENDS_WITH  : string = 'ends_with'
The field under this validation must end with another_field.
EQUAL  : string = 'equal'
Ensures a given value is **equal to** another field's value or to a fixed numeric constant.
EXTENSION  : string = 'extension'
The field under this rule must end with an extension corresponding to one of those listed.
FLOAT  : string = 'float'
The field under validation must be an float.
GREATER_THAN  : string = 'gt'
Ensures a given value is **greater than** another field's value or to a fixed numeric constant.
GREATER_THAN_OR_EQUAL  : string = 'gte'
Ensures a given value is **greater than or equal to** another field's value or to a fixed numeric constant.
IN  : string = 'in'
The field under this rule must be included in the given list of values.
INTEGER  : string = 'integer'
The field under validation must be an integer.
IP  : string = 'ipv'
The field under this rule must be a valid ipv4 or ipv6 address.
IPV4  : string = 'ipv4'
The field under this rule must be a valid ipv4 address.
IPV6  : string = 'ipv6'
The field under this rule must be a valid ipv6 address.
ISO8601_DATE_TIME  : string = 'iso8601_date_time'
The field under this rule must be a valid ISO 8601 date-time expression.
ISO8601_DATE_TIME_OR_DURATION  : string = 'iso8601_date_time_or_duration'
The field under this rule must be either a valid ISO 8601 date-time expression or a valid ISO 8601 duration expression.
ISO8601_DURATION  : string = 'iso8601_duration'
The field under this rule must be a valid ISO 8601 duration expression.
JSON  : string = 'json'
The field under this validation must be a valid JSON string.
LATITUDE  : string = 'latitude'
Validates that a value represents a valid geographic latitude.
LENGTH  : string = 'length'
The field under this validation must be a string of exactly the length specified.
LESS_THAN  : string = 'lt'
Ensures a given value is **less than** another field's value or to a fixed numeric constant.
LESS_THAN_OR_EQUAL  : string = 'lte'
Ensures a given value is **less than or equal to** another field's value or to a fixed numeric constant.
LONGITUDE  : string = 'longitude'
Validates that a value represents a valid geographic longitude.
LOWERCASE  : string = 'lowercase'
The field under this validation must be in lowercase.
MAX  : string = 'max'
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.
MIMES  : string = 'mimes'
The $_FILES item under validation must have a MIME type corresponding to one of the listed extensions.
MIN  : string = 'min'
The field under this rule must have a size greater than or equal to the given number.
NOT_IN  : string = 'not_in'
The field under this rule must not be included in the given list of values.
NULLABLE  : string = 'nullable'
The field under this rule may be empty.
PRESENT  : string = 'present'
The field under this rule must be in the set of inputs, whatever the value is.
PROHIBITED  : string = 'prohibited'
The field under this rule is not allowed.
PROHIBITED_IF  : string = 'prohibited_if'
The field under this rule is not allowed if another_field is provided with any of the value(s).
PROHIBITED_UNLESS  : string = 'prohibited_unless'
The field under this rule is not allowed unless another_field has one of these values.
RANGE  : string = 'range'
Validates that a numeric value lies between a minimum and a maximum value (inclusive).
REGEX  : string = 'regex'
The field under this rule must match the given regex.
REJECTED  : string = 'rejected'
The field under this rule must have a value that corresponds to rejection i.e. 0 (zero), "0", false, no, "false", off.
REQUIRED  : string = 'required'
The field under this validation must be present and contain a non-empty value.
REQUIRED_IF  : string = 'required_if'
The field under this rule must be present and not empty if the another_field field is equal to any value.
REQUIRED_UNLESS  : string = 'required_unless'
The field under validation must be present and not empty unless the another_field field is equal to any value.
REQUIRED_WITH  : string = 'required_with'
The field under validation must be present and not empty only if any of the other specified fields are present.
REQUIRED_WITH_ALL  : string = 'required_with_all'
The field under validation must be present and not empty only if all the other specified fields are present.
REQUIRED_WITHOUT  : string = 'required_without'
The field under validation must be present and not empty only when any of the other specified fields are not present.
REQUIRED_WITHOUT_ALL  : string = 'required_without_all'
The field under validation must be present and not empty only when all the other specified fields are not present.
REQUIRES  : string = 'requires'
The field under validation requires that the specified fields are present in the input data and are not empty.
SAME  : string = 'same'
The field value under this rule must have the same value as another_field.
SOMETIMES  : string = 'sometimes'
Sometimes attributes can be left off or can be null.
STARTS_WITH  : string = 'starts_with'
The field under this validation must start with another_field.
STRING  : string = 'string'
The field under this rule must be a PHP string.
UPPERCASE  : string = 'uppercase'
The field under this validation must be in uppercase.
URL  : string = 'url'
The field under this rule must be a valid url format.
UUID  : string = 'uuid'
The field under this validation must be a valid UUID and not the nil UUID string.

Constants

ACCEPTED

The field under this rule must be one of 'on', 'yes', '1', 'true' (the string "true"), or true.

public string ACCEPTED = 'accepted'

AFTER

The field under this rule must be a date after the given minimum.

public string AFTER = 'after'

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

Tags
example
after:next week
after:2016-12-31
after:2016
after:2016-12-31 09:56:02

ALPHA

The field under this rule must be entirely alphabetic characters.

public string ALPHA = 'alpha'

ALPHA_DASH

The field under this rule may have alpha-numeric characters, as well as dashes and underscores.

public string ALPHA_DASH = 'alpha_dash'

ALPHA_NUM

The field under this rule must be entirely alpha-numeric characters.

public string ALPHA_NUM = 'alpha_num'

ALPHA_SPACES

The field under this rule may have alpha characters, as well as spaces.

public string ALPHA_SPACES = 'alpha_spaces'

ANY_OF

A variation of in: here the values (separated by default with a ,) must all be in the given values.

public string ANY_OF = 'any'

ARRAY

The field under this rule must be an array.

public string ARRAY = 'array'

ARRAY_CAN_ONLY_HAVE_KEYS

The array can only contain the specified keys, any keys not present will fail validation.

public string ARRAY_CAN_ONLY_HAVE_KEYS = 'array_can_only_have_keys'

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.

Tags
example
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

ARRAY_MUST_HAVE_KEYS

The array must contain all the specified keys to be valid.

public string ARRAY_MUST_HAVE_KEYS = 'array_must_have_keys'

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.

Tags
example
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

BEFORE

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

public string BEFORE = 'before'

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

Tags
example
before:last week
before:2016-12-31
before:2016
before:2016-12-31 09:56:02

BETWEEN

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.

public string BETWEEN = 'between'

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

Tags
example
$validation = $validator->validate
(
    [ 'photo' => $_FILES['photo'] ],
    [ 'photo' => 'required|between:1M,2M' ]
);

BOOLEAN

The field under this rule must be boolean. Accepted inputs are true, false, 1, 0, "1", and "0".

public string BOOLEAN = 'boolean'

CALLBACK

Define a custom callback to validate the value. This rule cannot be registered using the string syntax.

public string CALLBACK = 'callback'

To use this rule, you must use the array syntax and either explicitly specify callback, or pass the closure:

Tags
example
$validation = $validator->validate( $_POST,
[
    'even_number' => [
         'required',
         function ( $value )
         {
              // false = invalid
              return (is_numeric($value) AND $value % 2 === 0);
         },
         'callback' => fn ($v) => is_numeric($v) && $v % 2 === 0,
     ]
]);

You can set a custom message by returning a string instead of false. To allow for message translation, instead of a literal string; return a message key instead and add this to the message bag on the Factory.

Note: returning a message string will be removed in a future version, requiring only boolean responses. Instead, set the message string directly before returning true/false via $this->message = "";

$validation = $validator->validate( $_POST,
[
   'even_number' =>
   [
        'required',
        function ($value)
        {
             if (!is_numeric($value))
             {
                 return ":attribute must be numeric.";
             }
             if ($value % 2 !== 0)
             {
                  return ":attribute is not even number.";
             }
             return true; // always return true if validation passes
        }
    ]
]);

COLOR

Ensures that a given value matches a valid color expression (e.g. "#ff0000").

public string COLOR = 'color'
Tags
see
ColorRule

DATE

The field under this rule must be valid date following a given format.

public string DATE = 'date'

Parameter format is optional, default format is Y-m-d.

DEFAULT

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

public string DEFAULT = 'default'
Tags
example
use Somnambulist\Components\Validation\Factory;

$validation = (new Factory)->validate
([
   'enabled' => null
],
[
    'enabled'   => 'default:1|required|in:0,1'
    'published' => 'default:0|required|in:0,1'
]);

$validation->passes(); // true

// Get the valid/default data
$valid_data = $validation->getValidData();

$enabled = $valid_data['enabled'];
$published = $valid_data['published'];

DIFFERENT

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

public string DIFFERENT = 'different'
Tags
example
different:another_field

DIGITS

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

public string DIGITS = 'digits'
Tags
example
digits:value

DIGITS_BETWEEN

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

public string DIGITS_BETWEEN = 'digits_between'
Tags
example
digits_between:min,max

ELEVATION

Ensures that a value represents a valid elevation (altitude) in meters.

public string ELEVATION = 'elevation'
Tags
see
ElevationRule

EMAIL

The field under this validation must be a valid email address according to the built-in PHP filter extension.

public string EMAIL = 'email'

See FILTER_VALIDATE_EMAIL for details.

ENDS_WITH

The field under this validation must end with another_field.

public string ENDS_WITH = 'ends_with'

Comparison can be against strings, numbers and array elements.

EQUAL

Ensures a given value is **equal to** another field's value or to a fixed numeric constant.

public string EQUAL = 'equal'
Tags
see
EqualRule

EXTENSION

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

public string EXTENSION = 'extension'

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

FLOAT

The field under validation must be an float.

public string FLOAT = 'float'

GREATER_THAN

Ensures a given value is **greater than** another field's value or to a fixed numeric constant.

public string GREATER_THAN = 'gt'
Tags
see
GreaterThanRule

GREATER_THAN_OR_EQUAL

Ensures a given value is **greater than or equal to** another field's value or to a fixed numeric constant.

public string GREATER_THAN_OR_EQUAL = 'gte'
Tags
see
GreaterThanOrEqualRule

IN

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

public string IN = 'in'

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])
   ]
]);

This rule uses in_array to perform the validation and by default does not perform strict checking. If you require strict checking, you can invoke the rule like this:

use Somnambulist\Components\Validation\Factory;

$factory = new Factory();
$validation = $factory->validate($data,
[
   'enabled' =>
   [
      'required',
      $factory->rule('in')->values([true, 1])->strict()
   ]
]);

INTEGER

The field under validation must be an integer.

public string INTEGER = 'integer'

IP

The field under this rule must be a valid ipv4 or ipv6 address.

public string IP = 'ipv'

IPV4

The field under this rule must be a valid ipv4 address.

public string IPV4 = 'ipv4'

IPV6

The field under this rule must be a valid ipv6 address.

public string IPV6 = 'ipv6'

ISO8601_DATE_TIME

The field under this rule must be a valid ISO 8601 date-time expression.

public string ISO8601_DATE_TIME = 'iso8601_date_time'
Tags
see
ISO8601DateTimeRule

ISO8601_DATE_TIME_OR_DURATION

The field under this rule must be either a valid ISO 8601 date-time expression or a valid ISO 8601 duration expression.

public string ISO8601_DATE_TIME_OR_DURATION = 'iso8601_date_time_or_duration'
Tags
see
ISO8601DateTimeOrDurationRule

ISO8601_DURATION

The field under this rule must be a valid ISO 8601 duration expression.

public string ISO8601_DURATION = 'iso8601_duration'
Tags
see
ISO8601DurationRule

JSON

The field under this validation must be a valid JSON string.

public string JSON = 'json'

LATITUDE

Validates that a value represents a valid geographic latitude.

public string LATITUDE = 'latitude'
Tags
see
LatitudeRule

LENGTH

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

public string LENGTH = 'length'

LESS_THAN

Ensures a given value is **less than** another field's value or to a fixed numeric constant.

public string LESS_THAN = 'lt'
Tags
see
LessThanRule

LESS_THAN_OR_EQUAL

Ensures a given value is **less than or equal to** another field's value or to a fixed numeric constant.

public string LESS_THAN_OR_EQUAL = 'lte'
Tags
see
LessThanOrEqualRule

LONGITUDE

Validates that a value represents a valid geographic longitude.

public string LONGITUDE = 'longitude'
Tags
see
LongitudeRule

LOWERCASE

The field under this validation must be in lowercase.

public string LOWERCASE = 'lowercase'

MAX

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.

public string MAX = 'max'

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

Tags
example
$validation = $validator->validate
([
    'photo' => $_FILES['photo']
],
[
    'photo' => 'required|max:2M'
]);

MIMES

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

public string MIMES = 'mimes'

MIN

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

public string MIN = 'min'

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:

Tags
example
$validation = $validator->validate
([
    'photo' => $_FILES['photo']
],
[
    'photo' => 'required|min:2M'
]);

NOT_IN

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

public string NOT_IN = 'not_in'

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

not_in:value_1,value_2,...

NULLABLE

The field under this rule may be empty.

public string NULLABLE = 'nullable'

PRESENT

The field under this rule must be in the set of inputs, whatever the value is.

public string PRESENT = 'present'

PROHIBITED

The field under this rule is not allowed.

public string PROHIBITED = 'prohibited'

PROHIBITED_IF

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

public string PROHIBITED_IF = 'prohibited_if'

PROHIBITED_UNLESS

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

public string PROHIBITED_UNLESS = 'prohibited_unless'

This is the inverse of prohibited_if.

RANGE

Validates that a numeric value lies between a minimum and a maximum value (inclusive).

public string RANGE = 'range'
Tags
see
RangeRule

REGEX

The field under this rule must match the given regex.

public string REGEX = '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)/'
   ]
])

REJECTED

The field under this rule must have a value that corresponds to rejection i.e. 0 (zero), "0", false, no, "false", off.

public string REJECTED = 'rejected'

This is the inverse of the accepted rule.

REQUIRED

The field under this validation must be present and contain a non-empty value.

public string REQUIRED = 'required'

Valid values include non-empty strings, numbers, and arrays with elements. Empty values such as null, empty arrays, or empty strings are considered invalid.

Examples:

Value Valid
'something' true
'0' true
0 true
[0] true
[null] true
null false
[] false
'' false

REQUIRED_IF

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

public string REQUIRED_IF = 'required_if'

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

required_if:another_field,value_1,value_2,...

REQUIRED_UNLESS

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

public string REQUIRED_UNLESS = 'required_unless'
required_unless:another_field,value_1,value_2,...

REQUIRED_WITH

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

public string REQUIRED_WITH = 'required_with'
required_with:field_1,field_2,...

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.

REQUIRED_WITH_ALL

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

public string REQUIRED_WITH_ALL = 'required_with_all'
required_with_all:field_1,field_2,...

REQUIRED_WITHOUT

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

public string REQUIRED_WITHOUT = 'required_without'

REQUIRED_WITHOUT_ALL

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

public string REQUIRED_WITHOUT_ALL = 'required_without_all'

REQUIRES

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

public string REQUIRES = 'requires'
requires:field_1,field_2,...

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.

SAME

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

public string SAME = 'same'

SOMETIMES

Sometimes attributes can be left off or can be null.

public string SOMETIMES = 'sometimes'

These cases should be handled carefully and have different results after validation.

Tags
example
[
    'filters' => 'sometimes|array',
]

STARTS_WITH

The field under this validation must start with another_field.

public string STARTS_WITH = 'starts_with'

Comparison can be against strings, numbers and array elements.

STRING

The field under this rule must be a PHP string.

public string STRING = 'string'

UPPERCASE

The field under this validation must be in uppercase.

public string UPPERCASE = 'uppercase'

URL

The field under this rule must be a valid url format.

public string URL = 'url'

The default is to validate the common format: any_scheme://.... You can specify specific URL schemes if you wish.

Tags
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://`
]);

UUID

The field under this validation must be a valid UUID and not the nil UUID string.

public string UUID = 'uuid'
On this page

Search results