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
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
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
binary()
Checks that a value is binary; either a '1' or a '0'.
public
static binary(mixed $val) : void
Parameters
- $val : mixed
Tags
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
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
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
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
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
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(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
numeric()
Checks that a value is numeric (integral or decimal)
public
static numeric(mixed $val) : void
Parameters
- $val : mixed
Tags
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
password()
Validate password by length, type of characters
public
static password(mixed $val) : void
Parameters
- $val : mixed
-
Password to validate
Tags
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
positiveInt()
Checks if a value is a positive integer
public
static positiveInt(mixed $val) : void
Parameters
- $val : mixed
-
Value to check
Tags
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
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
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(mixed $val) : void
Parameters
- $val : mixed
-
Value to check