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
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
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
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
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
binary()
Checks that a value is binary; either a '1' or a '0'.
public
static binary(string $val) : mixed
Parameters
- $val : string
Tags
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
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
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
email()
Validate email, commonly used characters only
public
static email(string $val) : mixed
Parameters
- $val : string
-
email address
Tags
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
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
boolinTable()
Checks a value matches an ID in a corresponding table
public
static inTable(string $val, string $table) : void
Parameters
- $val : string
- $table : string
Tags
ipv4Addr()
Checks that a value is a valid IPv4 address
public
static ipv4Addr(string $val) : void
Parameters
- $val : string
-
value
Tags
ipv4AddrOrCidr()
Checks that a value is a valid IPv4 address or CIDR block
public
static ipv4AddrOrCidr(string $val) : void
Parameters
- $val : string
-
value
Tags
ipv4Cidr()
Checks that a value is a valid IPv4 CIDR block
public
static ipv4Cidr(string $val) : void
Parameters
- $val : string
-
value
Tags
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
numeric()
Checks that a value is numeric (integral or decimal)
public
static numeric(string $val) : mixed
Parameters
- $val : string
Tags
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
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
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
positiveInt()
Checks if a value is a positive integer
public
static positiveInt(string $val) : mixed
Parameters
- $val : string
-
Value to check
Tags
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
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
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
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
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