Validate
in package
Validates an array of data.
Often used for POST requests.
Tags
Table of Contents
Constants
- AGREE = 'agree'
- ALPHANUMERIC = 'alphanumeric'
- AT_LEAST = 'at_least'
- AT_MOST = 'at_most'
- CUSTOM = 'custom'
- EMAIL = 'email'
- IN = 'in'
- IS_ACTIVE = 'isactive'
- IS_BANNED = 'isbanned'
- MATCHES = 'matches'
- MAX = 'max'
- MIN = 'min'
- NOT_CONTAIN = 'not_contain'
- NOT_START_WITH = 'not_start_with'
- NUMERIC = 'numeric'
- RATE_LIMIT = 'rate_limit'
- REGEX = 'regex'
- REQUIRED = 'required'
- TIMEZONE = 'timezone'
- UNIQUE = 'unique'
Properties
- $_db : DB
- $_errors : array<string|int, mixed>
- $_message : string|null
- $_messages : array<string|int, mixed>
- $_passed : bool
- $_to_convert : array<string|int, mixed>
Methods
- addCustomError() : void
- Add an array of information to generate an error message to the $_to_convert array.
- check() : Validate
- Validate an array of inputs.
- errors() : array<string|int, mixed>
- Translate temp error information to their specific or generic or fallback messages and return.
- message() : Validate
- Add generic message for any failures, specific `messages()` will override this.
- messages() : Validate
- Add custom messages to this `Validate` instance.
- passed() : bool
- Get if this `Validate` instance passed.
- __construct() : mixed
- Create new `Validate` instance.
- addError() : void
- Add an array of information to generate an error message to the $_to_convert array.
- getMessage() : string
- Get the error message for a field.
Constants
AGREE
public
string
AGREE
= 'agree'
Check the user has agreed to the terms and conditions
ALPHANUMERIC
public
string
ALPHANUMERIC
= 'alphanumeric'
Check that the value is alphanumeric
AT_LEAST
public
string
AT_LEAST
= 'at_least'
Check the numeric value is at least x
AT_MOST
public
string
AT_MOST
= 'at_most'
Check the numeric value is at most x
CUSTOM
public
string
CUSTOM
= 'custom'
Custom validation check
public
string
EMAIL
= 'email'
Check if email is valid
IN
public
string
IN
= 'in'
Check that the value is in of a set of values
IS_ACTIVE
public
string
IS_ACTIVE
= 'isactive'
Check that the specified user account is set as active (ie validated)
IS_BANNED
public
string
IS_BANNED
= 'isbanned'
Check that the specified user account is not banned
MATCHES
public
string
MATCHES
= 'matches'
Ensure provided value matches another
MAX
public
string
MAX
= 'max'
Define max number of characters
MIN
public
string
MIN
= 'min'
Define minimum number of characters
NOT_CONTAIN
public
string
NOT_CONTAIN
= 'not_contain'
Check that the value does not contain a pattern
NOT_START_WITH
public
string
NOT_START_WITH
= 'not_start_with'
Check that the value does not start with a pattern
NUMERIC
public
string
NUMERIC
= 'numeric'
Check that the value is numeric
RATE_LIMIT
public
string
RATE_LIMIT
= 'rate_limit'
Set a rate limit
REGEX
public
string
REGEX
= 'regex'
Check that the value matches a regex pattern
REQUIRED
public
string
REQUIRED
= 'required'
Ensure this field is not empty
TIMEZONE
public
string
TIMEZONE
= 'timezone'
Check that timezone is valid
UNIQUE
public
string
UNIQUE
= 'unique'
Check the value has not already been inputted in the database
Properties
$_db
private
DB
$_db
$_errors
private
array<string|int, mixed>
$_errors
= []
$_message
private
string|null
$_message
= \null
$_messages
private
array<string|int, mixed>
$_messages
= []
$_passed
private
bool
$_passed
= \false
$_to_convert
private
array<string|int, mixed>
$_to_convert
= []
Methods
addCustomError()
Add an array of information to generate an error message to the $_to_convert array.
public
addCustomError(string $item, string $error[, array<string|int, mixed> $meta = [] ]) : void
These errors will be translated in the errors()
function later.
Parameters
- $item : string
-
field item
- $error : string
-
error message
- $meta : array<string|int, mixed> = []
-
error metadata
check()
Validate an array of inputs.
public
static check(array<string|int, mixed> $source[, array<string|int, mixed> $items = [] ]) : Validate
Parameters
- $source : array<string|int, mixed>
-
inputs (eg: $_POST)
- $items : array<string|int, mixed> = []
-
subset of inputs to be validated
Tags
Return values
Validate —New instance of Validate.
errors()
Translate temp error information to their specific or generic or fallback messages and return.
public
errors() : array<string|int, mixed>
Return values
array<string|int, mixed> —Any and all errors for this Validate
instance.
message()
Add generic message for any failures, specific `messages()` will override this.
public
message(string $message) : Validate
Parameters
- $message : string
-
message to show if any failures occur.
Return values
Validate —This instance of Validate.
messages()
Add custom messages to this `Validate` instance.
public
messages(array<string|int, mixed> $messages) : Validate
Parameters
- $messages : array<string|int, mixed>
-
array of input names and strings or arrays to use as messages.
Return values
Validate —This instance of Validate.
passed()
Get if this `Validate` instance passed.
public
passed() : bool
Return values
bool —whether this 'Validate' passed or not.
__construct()
Create new `Validate` instance.
private
__construct() : mixed
addError()
Add an array of information to generate an error message to the $_to_convert array.
private
addError(array<string|int, mixed> $error) : void
These errors will be translated in the errors()
function later.
Parameters
- $error : array<string|int, mixed>
-
message to add to error array
getMessage()
Get the error message for a field.
private
getMessage(string $field, string $rule, string $fallback[, array<string|int, mixed>|null $meta = [] ]) : string
Priority:
- Message is set for the field and rule
- Message for field, not rule specific
- Result of callable if "*" rule exists
- Generic message set with
message(...)
- Fallback message for rule.
Parameters
- $field : string
-
name of field to search for.
- $rule : string
-
rule which check failed. should be from the constants defined above.
- $fallback : string
-
fallback default message if custom message and generic message are not supplied.
- $meta : array<string|int, mixed>|null = []
-
optional meta to provide to message.
Return values
string —Message for this field and rule.