RulesStaticValidator
in package
implements
RulesValidatorInterface
Rules based validation processor.
Used with the Validity class. This can be changed with setValidity().
Tags
Table of Contents
Interfaces
- RulesValidatorInterface
- This class is a validator that uses rules.
Properties
- $data : array<string|int, mixed>|object
- $field_errors : array<string|int, mixed>
- $general_errors : array<string|int, mixed>
- $labels : array<string|int, mixed>
- $rules : array<string|int, mixed>
- $validity : string
Methods
- __construct() : mixed
- addArrayFieldError() : void
- Add an error message for a given field to the field errors list This variation is for array validation, e.g. an array of integers
- addFieldError() : void
- Add an error message for a given field to the field errors list
- addGeneralError() : void
- Add a general error message, e.g. for errors affecting many fields
- addMultipleFieldError() : void
- Add an error message from a multiple-field validation (e.g. checking at least one is set)
- arrayCheck() : array<string|int, mixed>
- Run a validation check against each value in an array.
- check() : bool
- Check the value of a field against a validation method, storing any error messages received Additional arguments are passed to the underlying method
- errorsAsArray() : array<string|int, string>
- Return all errors (general and field) as a mapped array.
- fromValidationException() : void
- Populates form field errors from ValidationException
- getErrors() : array<string|int, mixed>
- Get validation errors (field errors).
- getFieldErrors() : array<string|int, mixed>
- Get an array of all field errors, indexed by field name Fields may have multiple errors defined
- getGeneralErrors() : array<string|int, mixed>
- Get an array of all general errors
- hasErrors() : bool
- Does this object have validation errors?
- isEmpty() : bool
- Sadly, the PHP builtin empty() considers '0' to be empty, but it actually isn't
- multipleCheck() : bool
- Check multiple fields against a validation method
- required() : void
- Checks various fields are required If a field is required and no value is provided, no other validation will be proessed for that field.
- setData() : void
- Update the data to validate
- setFieldLabel() : void
- Set the label for a single field
- setFieldValue() : void
- Set the value for a single data field
- setLabels() : void
- Field labels make error messages a little friendlier
- setRules() : void
- setValidity() : void
- Set the validity helper.
- trim() : array<string|int, mixed>
- Recursive trim data
- validate() : bool
- Validate properties in an object as defined the rules config.
- expandNs() : callable|false
- For a given function, search if it exists in the {@see Validity} class.
Properties
$data
protected
array<string|int, mixed>|object
$data
$field_errors
protected
array<string|int, mixed>
$field_errors
$general_errors
protected
array<string|int, mixed>
$general_errors
$labels
protected
array<string|int, mixed>
$labels
$rules
protected
array<string|int, mixed>
$rules
$validity
protected
string
$validity
Methods
__construct()
public
__construct(array<string|int, mixed>|object $data[, array<string|int, mixed> $rules = [] ]) : mixed
Parameters
- $data : array<string|int, mixed>|object
-
Data to validate
- $rules : array<string|int, mixed> = []
-
Validation rules
addArrayFieldError()
Add an error message for a given field to the field errors list This variation is for array validation, e.g. an array of integers
public
addArrayFieldError(string $field_name, int $index, string $message) : void
Parameters
- $field_name : string
-
The field to add the error message for
- $index : int
-
The array index of the field to report error for
- $message : string
-
The message text
addFieldError()
Add an error message for a given field to the field errors list
public
addFieldError(string $field_name, string $message) : void
Parameters
- $field_name : string
-
The field to add the error message for
- $message : string
-
The message text
addGeneralError()
Add a general error message, e.g. for errors affecting many fields
public
addGeneralError(string $message) : void
Parameters
- $message : string
-
The message text
addMultipleFieldError()
Add an error message from a multiple-field validation (e.g. checking at least one is set)
public
addMultipleFieldError(array<string|int, mixed> $fields, string $message) : void
Parameters
- $fields : array<string|int, mixed>
-
The fields to add the error message for
- $message : string
-
The message text
arrayCheck()
Run a validation check against each value in an array.
public
arrayCheck(string $field_name, callable|string $func, array<string|int, mixed> ...$args) : array<string|int, mixed>
Behaviour is very similar to the Validator::check method.
Only supports single-depth arrays
Errors are put into the field_errors array under a subkey matching the array key
Return value is an array of key => boolean with the validation result for each key
Parameters
- $field_name : string
-
The field to check
- $func : callable|string
-
The function or method to call.
- $args : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —Key => Boolean True if validation was successful, false if it failed
check()
Check the value of a field against a validation method, storing any error messages received Additional arguments are passed to the underlying method
public
check(string $field_name, callable|string $func, array<string|int, mixed> ...$args) : bool
If a field has already been checked with Validator::required and the field was empty, this function will not report errors (but will still return an appropriate value)
If a empty value is provided, it is not validated - returns true
Parameters
- $field_name : string
-
The field to check
- $func : callable|string
-
The function or method to call.
- $args : array<string|int, mixed>
Return values
bool —True if validation was successful, false if it failed
errorsAsArray()
Return all errors (general and field) as a mapped array.
public
errorsAsArray() : array<string|int, string>
Return values
array<string|int, string> —[ 'label' => 'messages' ]
fromValidationException()
Populates form field errors from ValidationException
public
fromValidationException(ValidationException $exception) : void
Parameters
- $exception : ValidationException
getErrors()
Get validation errors (field errors).
public
getErrors() : array<string|int, mixed>
This is to appease the interface gods.
Return values
array<string|int, mixed>getFieldErrors()
Get an array of all field errors, indexed by field name Fields may have multiple errors defined
public
getFieldErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>getGeneralErrors()
Get an array of all general errors
public
getGeneralErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>hasErrors()
Does this object have validation errors?
public
hasErrors() : bool
Return values
bool —True if there were any validation errors, false if there wasn't
isEmpty()
Sadly, the PHP builtin empty() considers '0' to be empty, but it actually isn't
public
static isEmpty(mixed $val) : bool
Parameters
- $val : mixed
Return values
bool —True if empty, false if not.
multipleCheck()
Check multiple fields against a validation method
public
multipleCheck(array<string|int, mixed> $fields, callable|string $func, array<string|int, mixed> ...$args) : bool
This is similar to Validator::check but it's designed for different validation methods, which work on a set of fields instead of a single field (e.g. Validity::oneRequired)
Additional arguments are passed to the underlying method
Parameters
- $fields : array<string|int, mixed>
-
The fields to check
- $func : callable|string
-
The function or method to call.
- $args : array<string|int, mixed>
Return values
bool —True if validation was successful, false if it failed
required()
Checks various fields are required If a field is required and no value is provided, no other validation will be proessed for that field.
public
required(array<string|int, mixed> $fields) : void
Parameters
- $fields : array<string|int, mixed>
-
Fields to check
setData()
Update the data to validate
public
setData(array<string|int, mixed>|object $data) : void
Parameters
- $data : array<string|int, mixed>|object
-
Data to validate
setFieldLabel()
Set the label for a single field
public
setFieldLabel(string $field, string $label) : void
Parameters
- $field : string
-
The field to set
- $label : string
-
The label to set on the field
setFieldValue()
Set the value for a single data field
public
setFieldValue(string $field, mixed $value) : void
Parameters
- $field : string
-
The field to set
- $value : mixed
-
The value to set on the field
setLabels()
Field labels make error messages a little friendlier
public
setLabels(array<string|int, mixed> $labels) : void
Parameters
- $labels : array<string|int, mixed>
-
Field labels
setRules()
public
setRules(array<string|int, mixed> $rules) : void
Parameters
- $rules : array<string|int, mixed>
setValidity()
Set the validity helper.
public
setValidity(string $class) : void
Parameters
- $class : string
Tags
trim()
Recursive trim data
public
static trim(array<string|int, mixed> &$data) : array<string|int, mixed>
Alters in-place AND returns the array This allows for use such as:
$_SESSION['register']['field_values'] = Validator::trim($_POST);
When used like this, the session gets set and the POST data is also trimmed, so can be used directly for database inserts.
Parameters
- $data : array<string|int, mixed>
-
Data to trim. Passed by-reference.
Return values
array<string|int, mixed> —Trimmed data
validate()
Validate properties in an object as defined the rules config.
public
validate() : bool
Tags
Return values
bool —True if valid. False if there were errors.
expandNs()
For a given function, search if it exists in the {@see Validity} class.
protected
expandNs(callable|string $func) : callable|false
Parameters
- $func : callable|string
-
The function to expand.
Return values
callable|false —False if not callable.