Documentation

Validity
in package

Validation class.

All methods should give useful errors by throwing a ValidationException.

Used with the RulesStaticValidator class.

Note, these methods accept mixed rather than strong 'string' types because often we're validating data from user input, which may contain nulls or other types.

Table of Contents

Methods

allInArray()  : mixed
Checks each value of an array is one of the allowed values
allMatch()  : void
All field values must match (e.g. password1 and password2 must match)
allUnique()  : void
All field values must be unique (e.g. home phone and work phone cannot be the same)
binary()  : void
Checks that a value is binary; either a '1' or a '0'.
dateMySQL()  : void
Checks if a value is a date in MySQL format (YYYY-MM-DD)
dateRange()  : void
Checks that a date range is valid.
datetimeMySQL()  : void
Checks if a value is a datetime in MySQL format (YYYY-MM-DD HH:MM:SS)
email()  : void
Validate email, commonly used characters only
inArray()  : void
Checks a value is one of the allowed values
ipv4Addr()  : void
Checks that a value is a valid IPv4 address
ipv4AddrOrCidr()  : void
Checks that a value is a valid IPv4 address or CIDR block
ipv4Cidr()  : void
Checks that a value is a valid IPv4 CIDR block
length()  : void
Checks the length of a string is within an allowed range
numeric()  : void
Checks that a value is numeric (integral or decimal)
oneRequired()  : void
At least one value must be specified (e.g. one of email/phone/mobile)
password()  : void
Validate password by length, type of characters
phone()  : void
Checks if a phone number is valid.
positiveInt()  : void
Checks if a value is a positive integer
proseText()  : void
Checks whether a string is made up of the kinds of characters that make up prose
range()  : void
Checks that a value is numeric (integral or decimal) and within a given inclusive range
regex()  : void
Checks that a value matches a regular expression
timeMySQL()  : void
Checks if a value is a time in MySQL format (HH:MM:SS)

Methods

allInArray()

Checks each value of an array is one of the allowed values

public static allInArray(array<string|int, mixed> $val, array<string|int, mixed> $allowed) : mixed
Parameters
$val : array<string|int, mixed>
$allowed : array<string|int, mixed>
Tags
example

$_POST['vowels'] = ['a', 'i'] $valid->check('vowel', 'allInArray', ['a', 'e', 'i', 'o', 'u'])

throws
ValidationException

allMatch()

All field values must match (e.g. password1 and password2 must match)

public static allMatch(array<string|int, mixed> $vals) : void
Parameters
$vals : array<string|int, mixed>

Values to check

Tags
example

$valid->multipleCheck(['password1', 'password2'], 'allMatch')

throws
ValidationException

allUnique()

All field values must be unique (e.g. home phone and work phone cannot be the same)

public static allUnique(array<string|int, mixed> $vals) : void
Parameters
$vals : array<string|int, mixed>

Values to check

Tags
example

$valid->multipleCheck(['home_phone', 'work_phone'], 'allUnique')

throws
ValidationException

binary()

Checks that a value is binary; either a '1' or a '0'.

public static binary(mixed $val) : void
Parameters
$val : mixed
Tags
example

$valid->check('active', 'binary')

throws
ValidationException

dateMySQL()

Checks if a value is a date in MySQL format (YYYY-MM-DD)

public static dateMySQL(mixed $val) : void
Parameters
$val : mixed

Value to check

Tags
example

$valid->check('date_published', 'dateMySQL')

throws
ValidationException

dateRange()

Checks that a date range is valid.

public static dateRange(array<string|int, mixed> $vals[, string $min = null ][, string $max = null ][, bool $enforce_ordering = true ]) : void
Parameters
$vals : array<string|int, mixed>

The values to check; there must be exactly two with the 'start' field name occuring first in the array

$min : string = null

(optional) A date string (compatible with strtotime) for the minimum of the date range.

$max : string = null

(optional) A date string (compatible with strtotime) for the maximum of the date range.

$enforce_ordering : bool = true

(optional) Ensures that the start date is less than or equal to the end date. On by default.

Tags
example

$valid->multipleCheck(['date_start', 'date_end'], 'Validity::dateRange', '1999-01-01', '2099-01-01')

datetimeMySQL()

Checks if a value is a datetime in MySQL format (YYYY-MM-DD HH:MM:SS)

public static datetimeMySQL(mixed $val) : void
Parameters
$val : mixed

Value to check

Tags
example

$valid->check('start_date', 'datetimeMySQL')

throws
ValidationException

email()

Validate email, commonly used characters only

public static email(mixed $val) : void

This checks for only one '@' and at least one 'dot' in the domain. This means test@localhost will not pass. This is intentional.

Parameters
$val : mixed

email address

Tags
example

$valid->check('email', 'email')

throws
ValidationException

inArray()

Checks a value is one of the allowed values

public static inArray(string $val, array<string|int, mixed> $allowed) : void
Parameters
$val : string
$allowed : array<string|int, mixed>
Tags
example

$valid->check('vowel', 'inArray', ['a', 'e', 'i', 'o', 'u'])

throws
ValidationException

ipv4Addr()

Checks that a value is a valid IPv4 address

public static ipv4Addr(string $val) : void
Parameters
$val : string

value

Tags
throws
ValidationException

If the value isn't a valid IPv4 address

ipv4AddrOrCidr()

Checks that a value is a valid IPv4 address or CIDR block

public static ipv4AddrOrCidr(string $val) : void
Parameters
$val : string

value

Tags
throws
ValidationException

If the value isn't a valid IPv4 address or CIDR block

ipv4Cidr()

Checks that a value is a valid IPv4 CIDR block

public static ipv4Cidr(string $val) : void
Parameters
$val : string

value

Tags
throws
ValidationException

If the value isn't a valid IPv4 CIDR block

length()

Checks the length of a string is within an allowed range

public static length(mixed $val, int $min[, int $max = PHP_INT_MAX ]) : void
Parameters
$val : mixed

The value

$min : int

Minimum length

$max : int = PHP_INT_MAX

Maximum length

Tags
example

$valid->check('name', 'length', 1, 100)

throws
ValidationException

If item is too short or too long

numeric()

Checks that a value is numeric (integral or decimal)

public static numeric(mixed $val) : void
Parameters
$val : mixed
Tags
example

$valid->check('cost', 'numeric')

throws
ValidationException

oneRequired()

At least one value must be specified (e.g. one of email/phone/mobile)

public static oneRequired(array<string|int, mixed> $vals) : void
Parameters
$vals : array<string|int, mixed>

Values to check

Tags
example

$valid->multipleCheck(['email', 'phone'], 'oneRequired')

throws
ValidationException

password()

Validate password by length, type of characters

public static password(mixed $val) : void
Parameters
$val : mixed

Password to validate

Tags
example

$valid->check('password', 'password')

throws
ValidationException

phone()

Checks if a phone number is valid.

public static phone(mixed $val[, int $min_digits = 8 ]) : void
Parameters
$val : mixed

Phone number

$min_digits : int = 8

Minimum number of digits required in phone number. This can be less than 8 for fields which allow short numbers like 000 or 13 11 66

Tags
example

$valid->check('mobile', 'phone', 10)

throws
ValidationException

positiveInt()

Checks if a value is a positive integer

public static positiveInt(mixed $val) : void
Parameters
$val : mixed

Value to check

Tags
example

$valid->check('region_id', 'positiveInt')

throws
ValidationException

proseText()

Checks whether a string is made up of the kinds of characters that make up prose

public static proseText(mixed $str) : void

Allowed: letters, numbers, space, punctuation Allowed punctuation: ' " / ! ? @ # $ % & ( ) - : ; . ,

Parameters
$str : mixed
Tags
example

$valid->check('name', 'proseText')

throws
ValidationException

range()

Checks that a value is numeric (integral or decimal) and within a given inclusive range

public static range(string $val, int|float $min, int|float $max) : void
Parameters
$val : string
$min : int|float

The minimum the value may be

$max : int|float

The maximum the value may be

Tags
example

$valid->check('cost', 'range', 0, 5000)

throws
ValidationException

regex()

Checks that a value matches a regular expression

public static regex(string $val, string $pattern) : void
Parameters
$val : string

value

$pattern : string

Regex pattern for preg_match. Consider starting with /^ and ending with $/

Tags
throws
ValidationException

If the value doesn't match the pattern

timeMySQL()

Checks if a value is a time in MySQL format (HH:MM:SS)

public static timeMySQL(mixed $val) : void
Parameters
$val : mixed

Value to check

Tags
example

$valid->check('event_time', 'timeMySQL')

throws
ValidationException
On this page

Search results