Documentation

Validity
in package

All of its methods should give useful errors by throwing a {@see ValidationException}.

Used with the Validator class.

Table of Contents

Methods

allInArray()  : mixed
Checks each value of an array is one of the allowed values
allInSet()  : mixed
Checks all selected values belong to a database SET definition
allInTable()  : void
Checks all selected values match IDs in a corresponding table
allMatch()  : mixed
All field values must match (e.g. password1 and password2 must match)
allUnique()  : mixed
All field values must be unique (e.g. home phone and work phone cannot be the same)
binary()  : mixed
Checks that a value is binary; either a '1' or a '0'.
dateMySQL()  : mixed
Checks if a value is a date in MySQL format (YYYY-MM-DD)
dateRange()  : mixed
Checks that a date range is valid.
datetimeMySQL()  : mixed
Checks if a value is a datetime in MySQL format (YYYY-MM-DD HH:MM:SS)
email()  : mixed
Validate email, commonly used characters only
inArray()  : mixed
Checks a value is one of the allowed values
inEnum()  : bool
Validates a value meant for an ENUM field
inTable()  : void
Checks a value matches an ID in a corresponding table
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()  : mixed
Checks the length of a string is within an allowed range
numeric()  : mixed
Checks that a value is numeric (integral or decimal)
oneRequired()  : mixed
At least one value must be specified (e.g. one of email/phone/mobile)
password()  : mixed
Validate password by length, type of characters, and list of common passwords
phone()  : mixed
Checks if a phone number is valid.
positiveInt()  : mixed
Checks if a value is a positive integer
proseText()  : mixed
Checks whether a string is made up of the kinds of characters that make up prose
range()  : mixed
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()  : mixed
Checks if a value is a time in MySQL format (HH:MM:SS)
uniqueValue()  : mixed
Checks that a unique value doesn't already exist in the database, e.g. a username or email address.

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', 'Validity::allInArray', ['a', 'e', 'i', 'o', 'u'])

throws
ValidationException

allInSet()

Checks all selected values belong to a database SET definition

public static allInSet(array<string|int, mixed> $val, string $table, string $col) : mixed
Parameters
$val : array<string|int, mixed>

The value

$table : string

The DB table which contains the SET column

$col : string

The SET column

Tags
example

$valid->check('days', 'Validity::allInSet', 'events', 'days')

throws
InvalidArgumentException

If the data isn't an array or the column isn't an ENUM or SET

ValidationException

If item is too short or too long

allInTable()

Checks all selected values match IDs in a corresponding table

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

$_POST['favourite_cities'] = (array) @$_POST['favourite_cities'] $valid->check('favourite_cities', 'Validity::allInTable', 'cities')

throws
ValidationException

allMatch()

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

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

Values to check

Tags
example

$valid->multipleCheck(['password1', 'password2'], 'Validity::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) : mixed
Parameters
$vals : array<string|int, mixed>

Values to check

Tags
example

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

throws
ValidationException

binary()

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

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

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

throws
ValidationException

dateMySQL()

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

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

Value to check

Tags
example

$valid->check('date_published', 'Validity::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 ]) : mixed
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(string $val) : mixed
Parameters
$val : string

Value to check

Tags
example

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

throws
ValidationException

email()

Validate email, commonly used characters only

public static email(string $val) : mixed
Parameters
$val : string

email address

Tags
example

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

throws
ValidationException

inArray()

Checks a value is one of the allowed values

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

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

throws
ValidationException

inEnum()

Validates a value meant for an ENUM field

public static inEnum(string $val, string $table, string $col) : bool
Parameters
$val : string
$table : string

The DB table which contains the ENUM column

$col : string

The ENUM column

Return values
bool

inTable()

Checks a value matches an ID in a corresponding table

public static inTable(string $val, string $table) : void
Parameters
$val : string
$table : string
Tags
example

$valid->check('user_id', 'Validity::inTable', 'users')

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(string $val, int $min[, int $max = PHP_INT_MAX ]) : mixed
Parameters
$val : string

The value

$min : int

Minimum length

$max : int = PHP_INT_MAX

Maximum length

Tags
example

$valid->check('name', 'Validity::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(string $val) : mixed
Parameters
$val : string
Tags
example

$valid->check('cost', 'Validity::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) : mixed
Parameters
$vals : array<string|int, mixed>

Values to check

Tags
example

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

throws
ValidationException

password()

Validate password by length, type of characters, and list of common passwords

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

Password to validate

Tags
example

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

throws
ValidationException

phone()

Checks if a phone number is valid.

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

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', 'Validity::phone', 10)

throws
ValidationException

positiveInt()

Checks if a value is a positive integer

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

Value to check

Tags
example

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

throws
ValidationException

proseText()

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

public static proseText(string $str) : mixed

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

Parameters
$str : string
Tags
example

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

throws
ValidationException

range()

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

public static range(string $val, number $min, number $max) : mixed
Parameters
$val : string
$min : number

The minimum the value may be

$max : number

The maximum the value may be

Tags
example

$valid->check('cost', 'Validity::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(string $val) : mixed
Parameters
$val : string

Value to check

Tags
example

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

throws
ValidationException

uniqueValue()

Checks that a unique value doesn't already exist in the database, e.g. a username or email address.

public static uniqueValue(string $val, string $table, string $column[, int $id = 0 ][, string $error_msg = '' ]) : mixed

This is to give a friendlier frontend to DB errors pertaining to UNIQUE constraints. N.B. this function uses LIKE for case-insensitive matching, so it's even stricter than a UNIQUE constraint.

Parameters
$val : string

Value to check

$table : string

Table to search for an extant matching value

$column : string

Column in specified table

$id : int = 0

The ID of the record being edited (0 if adding a new record). This prevents an exception being thrown when the record being edited itself is found.

$error_msg : string = ''

Error message to be included in the exception

Tags
example

$valid->check('code', 'Validity::uniqueValue', 'events', 'code')

$valid->check('email', 'Validity::uniqueValue', 'users', 'email', UserAuth::get_id())

throws
ValidationException
InvalidArgumentException

If $table or $column is invalid

On this page

Search results