Skip to content

Latest commit

 

History

History
executable file
·
1305 lines (723 loc) · 29.9 KB

4.2 - Rules.md

File metadata and controls

executable file
·
1305 lines (723 loc) · 29.9 KB

4.2 - Validation rules

Below is a list with all predefined validation rules.

Accepted Date Is empty Prohibits
Accepted if DateBetween Is false Regex
Accepted not empty Declined Is int Required
Active URL Declined if Is not null Required array keys (array)
After (date) Declined not empty Is null Required with
After or equal (date) Different Is string Required with all
Alpha Different with all Is true Required without
Alpha dash Digits Json Required without all
Alpha numeric Digits between Length Same
Alpha space Digits max Length between Same not
Before (date) Digits min Length max Scalar
Before or equal (date) Distinct Length min Starts not with
Between Divisible by Lowercase Starts with
CSS Color Email MAC address Time zone
Confirmed Ends not with Max Uppercase
Contains Ends with Min URL
Contains letter Equals Negative UUID
Contains mixed case Hexadecimal Negative or zero UUID v1
Contains not In Not in UUID v3
Contains number IP Nullable UUID v4
Contains symbol IP private Number UUID v5
Count (array) IP public Positive Words
Countable (array) IP v4 Positive or zero Words max
Count between (array) IP v6 Present Words min
Count max (array) Is array Prohibited
Count min (array) Is bool Prohibited if

Accepted

Checks if the data under validation is accepted. By default, the field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.

$validator->field('fieldName')->accepted();

You may also pass an array with values which are considered accepted:

$validator->field('fieldName')->accepted(['accept', 'agree', 'ok']);

Accepted if

The field under validation must be "yes", "on", "1", "true", 1, or true if another field's value is equal to a given value.

use KrisKuiper\Validator\Validator;

$data = ['field' => 'yes', 'other_field' => 'foo'];
$validator = new Validator($data);

$validator->field('fieldName')->acceptedIf('other_field', 'foo');

You may also provide the values which should be considered as accepted:

$validator
    ->field('fieldName')
    ->acceptedIf('other_field', 'foo', ['accepted', 'agreed', 'checked']);

See also the Accepted and Accepted not empty rules.

Accepted not empty

The field under validation must be "yes", "on", "1", "true", 1, or true if another field's value is not empty.

use KrisKuiper\Validator\Validator;

$data = ['field' => 'yes', 'other_field' => 'foo'];
$validator = new Validator($data);

$validator->field('fieldName')->acceptedNotEmpty('other_field');

You may also provide the values which should be considered as accepted:

$validator
    ->field('fieldName')
    ->acceptedNotEmpty('other_field', ['accepted', 'agreed', 'checked']);

See also the Accepted and Accepted if rules.

Active URL

Checks if the data under validation is an active URL.

$validator->field('fieldName')->activeURL();

After

Checks if the data under validation comes after a given date.

$validator->field('fieldName')->after('2000-01-01', 'Y-m-d');

See also the Before, Before or equal and After or equal rules.

After or equal

Checks if the data under validation comes after or is equal to a given date.

$validator->field('fieldName')->afterOrEqual('2000-01-01', 'Y-m-d');

See also the After, Before or equal and Before rules.

Alpha

Checks if the field under validation only contains alpha characters (a-z and A-Z).

$validator->field('fieldName')->alpha();

Alpha dash

Checks if the field under validation only contains letters (case-insensitive) and numbers, dashes and underscores.

$validator->field('fieldName')->alphaDash();

Alpha numeric

Checks if the field under validation only exists off letters (case-insensitive) and numbers.

$validator->field('fieldName')->alphaNumeric();

Alpha space

Checks if the field under validation only exists off letters (case-insensitive) and spaces.

$validator->field('fieldName')->alphaSpace();

Before

Checks if the data under validation comes before a given date.

$validator->field('fieldName')->before('2030-01-01', 'Y-m-d');

See also the After, Before or equal and After or equal rules.

Before or equal

Checks if the data under validation comes before or is equal to a given date.

$validator->field('fieldName')->beforeOrEqual('2030-01-01', 'Y-m-d');

See also the After, After or equal and Before rules.

Between

Checks if the data under validation (number) is between a given minimum and maximum.

$validator->field('fieldName')->between(5, 10.5);

Confirmed

Checks if the field under validation has a matching field of {field}_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

$validator->field('fieldName')->confirmed();

Contains

Checks if the data under validation contains a given value.

$validator->field('fieldName')->contains('abc');

Use the second parameter to search case-sensitive:

$validator->field('fieldName')->contains('ABC', true);

See also the Contains not rule.

Contains not

Checks if the data under validation does not contain a given value.

$validator->field('fieldName')->containsNot('ABC');

Use the second parameter to search case-sensitive:

$validator->field('fieldName')->containsNot('ABC', true);

See also the Contains rule.

Contains letter

Checks if the data under validation has at least one letter.

$validator->field('fieldName')->containsLetter();

You can also provide the minimum amount of letters that is required:

$validator->field('fieldName')->containsLetter(3);

Contains mixed case

Checks if the data under validation has at least one uppercase and one lowercase letter.

$validator->field('fieldName')->containsMixedCase();

You can also provide the minimum amount of lowercase and uppercase letters that is required:

$validator->field('fieldName')->containsMixedCase(3, 4);

Contains digit

Checks if the data under validation has at least one digit.

$validator->field('fieldName')->containsDigit();

You can also provide the minimum amount of digits that is required:

$validator->field('fieldName')->containsDigit(2);

Contains symbol

Checks if the data under validation has at least one symbol.

$validator->field('fieldName')->containsSymbol();

You can also provide the minimum amount of symbols that is required:

$validator->field('fieldName')->containsSymbol(2);

Count

Checks if the data under validation contains a given amount of items.

$validator->field('fieldName')->count(10);

Countable

Checks if the data under validation is countable.

$validator->field('fieldName')->countable();

Count between

Checks if the data under validation contains an amount of items between a given minimum and maximum.

$validator->field('fieldName')->countBetween(5, 10);

Count max

Checks if the data under validation contains no more items than a given maximum amount.

$validator->field('fieldName')->countMax(10);

Count min

Checks if the data under validation contains at least a given amount of items.

$validator->field('fieldName')->countMin(5);

CSS Color

Checks if the data under validation is a valid CSS color. By default, a valid CSS color may or may not begin with a hash sign (#) and may consist of 6 or 3 hexadecimal characters.

$validator->field('fieldName')->cssColor();

You can define if the hash sign (#) is required or not (#ffffff is valid but ffffff is not):

$validator->field('fieldName')->cssColor(true);

You can also define that shortcodes are not accepted (i.e. #ffffff instead of #fff):

$validator->field('fieldName')->cssColor(true, false);

Date

Checks if the data under validation is a valid date.

$validator->field('fieldName')->date('Y-m-d');

Date between

Checks if the data under validation comes between a provided from and to date.

$validator->field('fieldName')->dateBetween('2000-01-01', '2010-01-01', 'Y-m-d');

Declined

Checks if the data under validation is declined. By default, the field under validation must be "no", "off", "0", "false", 0, or false.

$validator->field('fieldName')->declined();

You may also pass an array with values which are considered declined:

$validator->field('fieldName')->declined(['decline', 'disagree']);

Declined if

The field under validation must be "no", "off", "0", "false", 0, or false if another field's value is equal to a given value.

use KrisKuiper\Validator\Validator;

$data = ['field' => 'no', 'other_field' => 'foo'];
$validator = new Validator($data);

$validator->field('fieldName')->declinedIf('other_field', 'foo');

You may also provide the values which should be considered as declined:

$validator
    ->field('fieldName')
    ->declinedIf('other_field', 'foo', ['declined', 'disagree']);

See also the Declined and Declined not empty rules.

Declined not empty

The field under validation must be "no", "off", "0", "false", 0, or false if another field's value is not empty.

use KrisKuiper\Validator\Validator;

$data = ['field' => 'yes', 'other_field' => 'foo'];
$validator = new Validator($data);

$validator->field('fieldName')->declinedNotEmpty('other_field');

You may also provide the values which should be considered as declined:

$validator
    ->field('fieldName')
    ->declinedNotEmpty('other_field', ['declined', 'agreed', 'checked']);

See also the Declined and Declined if rules.

Different

Check if the data under validation does not match one of the values of one or more fields.

$validator->field('fieldName')->different('otherFieldName', 'anotherFieldName');

Different with all

Check if the data under validation does not match all the values of one or more fields.

$validator->field('fieldName')->differentWithAll('otherFieldName', 'anotherFieldName');

Digits

Check if an integer value have exact length of provided digits.

$validator->field('fieldName')->digits(5);

Digits between

Check if an integer value is between the provided min and max length of digits.

$validator->field('fieldName')->digitsBetween(4, 6);

Digits max

Check if an integer value has a maximum length of digits.

$validator->field('fieldName')->digitsMax(5);

Digits min

Check if an integer value has at least the provided length of digits.

$validator->field('fieldName')->digitsMin(5);

Distinct

Check if all the values in an array are unique.

$validator->field('fieldName')->distinct();

Divisible by

Check if the field under validation is divisible by a provided number

$validator->field('fieldName')->divisible(10);

You can use the strict parameter to strictly check if a value is divisible

$validator->field('fieldName')->divisible(10, true);

Email

Checks if the data under validation is a valid email address.

$validator->field('fieldName')->email();

Ends not with

Checks if the data under validation does not end with a given value.

$validator->field('fieldName')->endsNotWith('abc');

Use the second parameter to match case-sensitive:

$validator->field('fieldName')->endsNotWith('ABC', true);

See also the Ends with, Starts with and Starts not with rules.

Ends with

Checks if the data under validation ends with a given value.

$validator->field('fieldName')->endsWith('abc');

Use the second parameter to match case-sensitive:

$validator->field('fieldName')->endsWith('ABC', true);

See also the Ends not with, Starts with and Starts not with rules.

Equals

Checks if the data under validation equals a provided value.

$validator->field('fieldName')->equals('abc');

Use the second parameter to match case-sensitive:

$validator->field('fieldName')->equals('ABC', true);

Hexadecimal

Checks if the data under validation is a valid hexadecimal string

$validator->field('fieldName')->hexadecimal();

In

Checks if the data under validation exists in a given array.

$validator->field('fieldName')->in(['foo', 'bar']);

Use the second parameter to search type safe:

$validator->field('fieldName')->in(['123', 123], true);

See also the Not in rule.

IP

Checks if the data under validation is a valid IP address (v4 or v6).

$validator->field('fieldName')->ip();

IP private

Checks if the data under validation is a private IP address (v4 or v6).

$validator->field('fieldName')->ipPrivate();

IP public

Checks if the data under validation is a public ip address (v4 or v6).

$validator->field('fieldName')->ipPublic();

IP v4

Checks if the data under validation is a valid IP v4 address.

$validator->field('fieldName')->ipv4();

IP v6

Checks if the data under validation is a valid IP v6 address.

$validator->field('fieldName')->ipv6();

Is array

Checks if the data under validation is an array.

$validator->field('fieldName')->isArray();

Is boolean

Checks if the data under validation is a boolean true or false.

$validator->field('fieldName')->isBool();

Is empty

Checks if the data under validation is empty. Empty string, empty array and null are considered empty.

$validator->field('fieldName')->isEmpty();

Is false

Checks if the data under validation is boolean false.

$validator->field('fieldName')->isFalse();

Is int

Checks if the data under validation is an integer number.

$validator->field('fieldName')->isInt();

You can use the strict parameter to strictly check if a value is an integer number:

$validator->field('fieldName')->isInt(true);

Is not null

Checks if the data under validation is not null.

$validator->field('fieldName')->isNotNull();

See also the Is null rule.

Is null

Checks if the data under validation is null.

$validator->field('fieldName')->isNull();

See also the Is not null rule.

Is string

Checks if the data under validation is of the type string.

$validator->field('fieldName')->isString();

Is true

Checks if value equals boolean true.

$validator->field('fieldName')->isTrue();

JSON

Checks if the data under validation is valid JSON.

$validator->field('fieldName')->json();

Length

Checks if the value character length is the given length.

$validator->field('fieldName')->length(10);

Length between

Checks if the data under validation is a valid URL.

$validator->field('fieldName')->lengthBetween(10, 20);

Length max

Checks if the amount of characters is less or equal than the given amount.

$validator->field('fieldName')->lengthMax(10);

Length min

Checks if the amount of characters is at least a given amount.

$validator->field('fieldName')->lengthMin(5);

Lowercase

Checks if the data under validation is a string containing only lowercase characters.

$validator->field('fieldName')->lowercase();

MAC address

Checks if the data under validation is a valid MAC address. By default, the dash "-" symbol is used as the delimiter for a valid MAC address.

$validator->field('fieldName')->macAddress();

You can change the delimiter i.e. colon:

$validator->field('fieldName')->macAddress(':');

Max

Checks if the value is less than the given maximum amount.

$validator->field('fieldName')->max(10.5);

Min

Checks if the field under validation is at least a given minimum.

$validator->field('fieldName')->min(10.5;

Not in

Checks if the data under validation exists in a given array.

$validator->field('fieldName')->notIn(['foo', 'bar']);

Use the second parameter to search type safe:

$validator->field('fieldName')->notIn(['123', 123], true);

Negative

Check if the field under validation is a negative number

$validator->field('fieldName')->negative();

You can use the strict parameter to strictly check if a value is negative

$validator->field('fieldName')->negative(true);

Negative or zero

Check if the field under validation is a negative number or is equals to zero.

$validator->field('fieldName')->negativeOrZero();

You can use the strict parameter to strictly check if a value is negative or equals to zero.

$validator->field('fieldName')->negativeOrZero(true);

Nullable

The field under validation may be null. If the value is null, then all other rules are bailed (will not be executed).

$validator->field('fieldName')->nullable();

You can also define that the field under validation may not be null:

$validator->field('fieldName')->nullable(false);

Number

Checks if the data under validation is an integer number.

$validator->field('fieldName')->number();

You can use the strict parameter to strictly check if a value is a number:

$validator->field('fieldName')->number(true);

Positive

Check if the field under validation is a positive number.

$validator->field('fieldName')->positive();

You can use the strict parameter to strictly check if a value is positive.

$validator->field('fieldName')->positive(true);

Positive or zero

Check if the field under validation is a positive number or is equals to zero.

$validator->field('fieldName')->positiveOrZero();

You can use the strict parameter to strictly check if a value is positive or equals to zero.

$validator->field('fieldName')->positiveOrZero(true);

Present

Check if the data under validation exists as key.

$validator->field('fieldName')->present();

Prohibited

Checks if the field under validation is an empty string or not present.

$validator->field('fieldName')->prohibited();

Prohibited if

Checks if the field under validation is considered empty (null, empty string or empty array) or not present if another field is equal to any value.

$validator->field('fieldName')->prohibitedIf('otherFieldName');

Prohibits

Checks if the field under validation is present. If so, the provided field name(s) may not be present, even if empty.

$validator->field('fieldName')->prohibits('otherFieldName');

Regex

Check if value matches a regular expression pattern.

$validator->field('fieldName')->regex('/[a-z]+/');

Required

Adds a new rule that will require the field/value (null, '', [] are considered empty).

$validator->field('fieldName')->required();

Required array keys

Checks if the field under validation is an array and contains at least the specified keys.

$validator->field('fieldName')->requiredArrayKeys('foo', 'bar');

Required with

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

$validator->field('fieldName')->requiredWith('otherFieldName', 'anotherFieldName');

Required with all

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

$validator->field('fieldName')->requiredWithAll('otherFieldName', 'anotherFieldName');

Required without

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

$validator->field('fieldName')->requiredWithout('otherFieldName', 'anotherFieldName');

Required without all

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

$validator->field('fieldName')->requiredWithoutAll('otherFieldName', 'anotherFieldName');

Same

Check if value matches a value of a given field name.

$validator->field('password')->same('password-repeat');

Same not

Check if value does not match a value of a given field name.

$validator->field('field')->sameNot('other-field');

Scalar

Checks if the data under validation is a scalar type.

$validator->field('fieldName')->scalar();

Starts not with

Checks if the data under validation does not begin with a given value.

$validator->field('fieldName')->startsNotWith('abc');

Use the second parameter to search case-sensitive:

$validator->field('fieldName')->startsNotWith('ABC', true);

See also the Starts with, Ends with and Ends not with rules.

Starts with

Checks if the data under validation begins with a given value.

$validator->field('fieldName')->startsWith('abc');

Use the second parameter to search case-sensitive:

$validator->field('fieldName')->startsWith('ABC', true);

See also the Ends with, Ends not with and Starts not with rules.

Time zone

Checks if the data under validation is a valid time zone.

$validator->field('fieldName')->timezone();

Use the first parameter to search case-insensitive:

$validator->field('fieldName')->timezone(true);

Note: see timezones on php.net for more information.

Uppercase

Checks if the data under validation is a string containing only uppercase characters.

$validator->field('fieldName')->uppercase();

URL

Checks if the data under validation is a valid URL. By default, the protocol will not be checked.

$validator->field('fieldName')->url();

Force the protocol (i.e. http or https):

$validator->field('fieldName')->url(true);

UUID

Checks if the data under validation is a valid UUID v1, v3, v4 or v5 entity.

$validator->field('fieldName')->uuid();

UUID v1

Checks if the data under validation is a valid UUID v1 entity.

$validator->field('fieldName')->uuidv1();

UUID v3

Checks if the data under validation is a valid UUID v3 entity.

$validator->field('fieldName')->uuidv3();

UUID v4

Checks if the data under validation is a valid UUID v4 entity.

$validator->field('fieldName')->uuidv4();

UUID v5

Checks if the data under validation is a valid UUID v4 entity.

$validator->field('fieldName')->uuidv5();

Words

Checks if the amount of words is at least to a given amount. By default, a word is defined to have two or more alphanumeric characters.

$validator->field('fieldName')->words(10);

The second parameter defines the minimum length of the word, while the third parameter can be used to allow all symbols instead of only alphanumeric characters:

$validator->field('fieldName')->words(10, 5, false);

See also the Words max and Words min rules.

Words max

Checks if the amount of words is less than or equal to a given amount. By default, a word is defined to have two or more alphanumeric characters.

$validator->field('fieldName')->wordsMax(10);

The second parameter defines the minimum length of the word, while the third parameter can be used to allow all symbols instead of only alphanumeric characters:

$validator->field('fieldName')->wordsMax(10, 5, false);

Words min

Checks if the amount of words is more than or equal to a given amount. By default, a word is defined to have two or more alphanumeric characters.

$validator->field('fieldName')->wordsMin(10);

The second parameter defines the minimum length of the word, while the third parameter can be used to allow all symbols instead of only alphanumeric characters:

$validator->field('fieldName')->wordsMin(10, 5, false);

Go to the previous section.

Go to the next section.