Validation
in package
implements
ValidationInterface
Validator
Tags
Table of Contents
Interfaces
- ValidationInterface
- Expected behavior of a validator
Properties
- $config : Validation
- Our configuration.
- $customErrors : array<string|int, mixed>
- Stores custom error message to use during validation. Where 'key' is the alias.
- $data : array<string|int, mixed>
- The data that should be validated, where 'key' is the alias, with value.
- $errors : array<string|int, mixed>
- Any generated errors during validation.
-
$rules
: array<string|int, array{label?: string, rules: list
}> - Stores the actual rules that should be run against $data.
- $ruleSetFiles : array<string|int, mixed>
- Files to load with validation functions.
- $ruleSetInstances : array<string|int, mixed>
- The loaded instances of our validation files.
- $validated : array<string|int, mixed>
- The data that was actually validated.
- $view : RendererInterface
- The view renderer used to render validation messages.
Methods
- __construct() : mixed
- Validation constructor.
- check() : bool
- Runs the validation process, returning true or false determining whether validation was successful or not.
- getError() : string
- Returns the error(s) for a specified $field (or empty string if not set).
- getErrors() : array<string, string>
- Returns the array of errors that were encountered during a run() call. The array should be in the following format:
- getRuleGroup() : array<int, string>
- Get rule group.
- getRules() : array<string|int, mixed>
- Returns all of the rules currently defined.
- getValidated() : array<string|int, mixed>
- Returns the actual validated data.
- hasError() : bool
- Checks to see if an error exists for the given field.
- hasRule() : bool
- Checks to see if the rule for key $field has been set or not.
- listErrors() : string
- Returns the rendered HTML of the errors as defined in $template.
- loadRuleGroup() : array<int, array<string|int, mixed>>
- Loads custom rule groups (if set) into the current rules.
- reset() : ValidationInterface
- Resets the class to a blank slate. Should be called whenever you need to process more than one array.
- run() : bool
- Runs the validation process, returning true/false determining whether validation was successful or not.
- setError() : ValidationInterface
- Sets the error for a specific field. Used by custom validation methods.
- setRule() : $this
- Sets (or adds) an individual rule and custom error messages for a single field.
- setRuleGroup() : void
- Set rule group.
- setRules() : ValidationInterface
- Stores the rules that should be used to validate the items.
- showError() : string
- Displays a single error in formatted HTML as defined in the $template view.
- withRequest() : ValidationInterface
- Takes a Request object and grabs the input data to use from its array values.
- fillPlaceholders() : array<string|int, mixed>
- Replace any placeholders within the rules with the values that match the 'key' of any properties being set. For example, if we had the following $data array:
- getErrorMessage() : string
- Attempts to find the appropriate error message
- loadRuleSets() : void
- Loads all of the rulesets classes that have been defined in the Config\Validation and stores them locally so we can use them.
- processRules() : bool
- Runs all of $rules against $field, until one fails, or all of them have been processed. If one fails, it adds the error to $this->errors and moves on to the next, so that we can collect all of the first errors.
- splitRules() : array<string|int, mixed>
- Split rules string by pipe operator.
- getRegex() : string
- Returns regex pattern for key with dot array syntax.
- isClosure() : bool
- isStringList() : bool
- Is the array a string list `list<string>`?
- processIfExist() : array<string|int, mixed>|true
- processPermitEmpty() : array<string|int, mixed>|true
- retrievePlaceholders() : array<string|int, mixed>
- Retrieves valid placeholder fields.
Properties
$config
Our configuration.
protected
Validation
$config
$customErrors
Stores custom error message to use during validation. Where 'key' is the alias.
protected
array<string|int, mixed>
$customErrors
= []
$data
The data that should be validated, where 'key' is the alias, with value.
protected
array<string|int, mixed>
$data
= []
$errors
Any generated errors during validation.
protected
array<string|int, mixed>
$errors
= []
'key' is the alias, 'value' is the message.
$rules
Stores the actual rules that should be run against $data.
protected
array<string|int, array{label?: string, rules: list}>
$rules
= []
[ field1 => [ 'label' => label, 'rules' => [ rule1, rule2, ... ], ], ]
$ruleSetFiles
Files to load with validation functions.
protected
array<string|int, mixed>
$ruleSetFiles
$ruleSetInstances
The loaded instances of our validation files.
protected
array<string|int, mixed>
$ruleSetInstances
= []
$validated
The data that was actually validated.
protected
array<string|int, mixed>
$validated
= []
$view
The view renderer used to render validation messages.
protected
RendererInterface
$view
Methods
__construct()
Validation constructor.
public
__construct(Validation $config, RendererInterface $view) : mixed
Parameters
- $config : Validation
- $view : RendererInterface
check()
Runs the validation process, returning true or false determining whether validation was successful or not.
public
check(array<string|int, mixed>|bool|float|int|object|string|null $value, array<string|int, mixed>|string $rules[, array<int, string> $errors = [] ][, string|null $dbGroup = null ]) : bool
Parameters
- $value : array<string|int, mixed>|bool|float|int|object|string|null
-
The data to validate.
- $rules : array<string|int, mixed>|string
-
The validation rules.
- $errors : array<int, string> = []
-
The custom error message.
- $dbGroup : string|null = null
-
The database group to use.
Return values
bool —True if valid, else false.
getError()
Returns the error(s) for a specified $field (or empty string if not set).
public
getError([string|null $field = null ]) : string
Parameters
- $field : string|null = null
Return values
stringgetErrors()
Returns the array of errors that were encountered during a run() call. The array should be in the following format:
public
getErrors() : array<string, string>
[ 'field1' => 'error message', 'field2' => 'error message', ]
Tags
Return values
array<string, string>getRuleGroup()
Get rule group.
public
getRuleGroup(string $group) : array<int, string>
Parameters
- $group : string
-
Group.
Tags
Return values
array<int, string> —Rule group.
getRules()
Returns all of the rules currently defined.
public
getRules() : array<string|int, mixed>
Return values
array<string|int, mixed>getValidated()
Returns the actual validated data.
public
getValidated() : array<string|int, mixed>
Return values
array<string|int, mixed>hasError()
Checks to see if an error exists for the given field.
public
hasError(string $field) : bool
Parameters
- $field : string
Return values
boolhasRule()
Checks to see if the rule for key $field has been set or not.
public
hasRule(string $field) : bool
Parameters
- $field : string
Return values
boollistErrors()
Returns the rendered HTML of the errors as defined in $template.
public
listErrors([string $template = 'list' ]) : string
You can also use validation_list_errors() in Form helper.
Parameters
- $template : string = 'list'
Return values
stringloadRuleGroup()
Loads custom rule groups (if set) into the current rules.
public
loadRuleGroup([non-empty-string|null $group = null ]) : array<int, array<string|int, mixed>>
Rules can be pre-defined in Config\Validation and can be any name, but must all still be an array of the same format used with setRules(). Additionally, check for {group}_errors for an array of custom error messages.
Parameters
- $group : non-empty-string|null = null
Tags
Return values
array<int, array<string|int, mixed>> —[rules, customErrors]
reset()
Resets the class to a blank slate. Should be called whenever you need to process more than one array.
public
reset() : ValidationInterface
Return values
ValidationInterfacerun()
Runs the validation process, returning true/false determining whether validation was successful or not.
public
run([array<string|int, mixed>|null $data = null ][, string|null $group = null ][, array<string|int, mixed>|BaseConnection|non-empty-string|null $dbGroup = null ]) : bool
Parameters
- $data : array<string|int, mixed>|null = null
-
The array of data to validate.
- $group : string|null = null
-
The predefined group of rules to apply.
- $dbGroup : array<string|int, mixed>|BaseConnection|non-empty-string|null = null
-
The database group to use.
Return values
boolsetError()
Sets the error for a specific field. Used by custom validation methods.
public
setError(string $field, string $error) : ValidationInterface
Parameters
- $field : string
- $error : string
Return values
ValidationInterfacesetRule()
Sets (or adds) an individual rule and custom error messages for a single field.
public
setRule(string $field, string|null $label, array<string|int, mixed>|string $rules[, array<string|int, mixed> $errors = [] ]) : $this
The custom error message should be just the messages that apply to this field, like so: [ 'rule1' => 'message1', 'rule2' => 'message2', ]
Parameters
- $field : string
- $label : string|null
- $rules : array<string|int, mixed>|string
-
The validation rules.
- $errors : array<string|int, mixed> = []
-
The custom error message.
Tags
Return values
$thissetRuleGroup()
Set rule group.
public
setRuleGroup(string $group) : void
Parameters
- $group : string
-
Group.
Tags
setRules()
Stores the rules that should be used to validate the items.
public
setRules(array<string|int, mixed> $rules[, array<string|int, mixed> $errors = [] ]) : ValidationInterface
Rules should be an array formatted like: [ 'field' => 'rule1|rule2' ]
The $errors array should be formatted like: [ 'field' => [ 'rule1' => 'message1', 'rule2' => 'message2', ], ]
Parameters
- $rules : array<string|int, mixed>
- $errors : array<string|int, mixed> = []
-
An array of custom error messages
Return values
ValidationInterfaceshowError()
Displays a single error in formatted HTML as defined in the $template view.
public
showError(string $field[, string $template = 'single' ]) : string
You can also use validation_show_error() in Form helper.
Parameters
- $field : string
- $template : string = 'single'
Return values
stringwithRequest()
Takes a Request object and grabs the input data to use from its array values.
public
withRequest(RequestInterface $request) : ValidationInterface
Parameters
- $request : RequestInterface
Return values
ValidationInterfacefillPlaceholders()
Replace any placeholders within the rules with the values that match the 'key' of any properties being set. For example, if we had the following $data array:
protected
fillPlaceholders(array<string|int, mixed> $rules, array<string|int, mixed> $data) : array<string|int, mixed>
[ 'id' => 13 ]
and the following rule:
'is_unique[users,email,id,{id}]'
The value of {id} would be replaced with the actual id in the form data:
'is_unique[users,email,id,13]'
Parameters
- $rules : array<string|int, mixed>
- $data : array<string|int, mixed>
Return values
array<string|int, mixed>getErrorMessage()
Attempts to find the appropriate error message
protected
getErrorMessage(string $rule, string $field[, non-empty-string|null $label = null ][, string|null $param = null ][, string|null $value = null ][, string|null $originalField = null ]) : string
Parameters
- $rule : string
- $field : string
- $label : non-empty-string|null = null
- $param : string|null = null
- $value : string|null = null
-
The value that caused the validation to fail.
- $originalField : string|null = null
Return values
stringloadRuleSets()
Loads all of the rulesets classes that have been defined in the Config\Validation and stores them locally so we can use them.
protected
loadRuleSets() : void
processRules()
Runs all of $rules against $field, until one fails, or all of them have been processed. If one fails, it adds the error to $this->errors and moves on to the next, so that we can collect all of the first errors.
protected
processRules(string $field, string|null $label, array<string|int, mixed>|string $value[, array<string|int, mixed> $rules = null ][, array<string|int, mixed> $data = null ][, string|null $originalField = null ]) : bool
Parameters
- $field : string
- $label : string|null
- $value : array<string|int, mixed>|string
- $rules : array<string|int, mixed> = null
- $data : array<string|int, mixed> = null
-
The array of data to validate, with
DBGroup
. - $originalField : string|null = null
-
The original asterisk field name like "foo.*.bar".
Return values
boolsplitRules()
Split rules string by pipe operator.
protected
splitRules(string $rules) : array<string|int, mixed>
Parameters
- $rules : string
Return values
array<string|int, mixed>getRegex()
Returns regex pattern for key with dot array syntax.
private
static getRegex(string $field) : string
Parameters
- $field : string
Return values
stringisClosure()
private
isClosure(callable(Array, Array, Array, Array): Array $rule) : bool
Parameters
- $rule : callable(Array, Array, Array, Array): Array
Return values
boolisStringList()
Is the array a string list `list<string>`?
private
isStringList(array<string|int, mixed> $array) : bool
Parameters
- $array : array<string|int, mixed>
Return values
boolprocessIfExist()
private
processIfExist(string $field, array<string|int, mixed> $rules, array<string|int, mixed> $data) : array<string|int, mixed>|true
Parameters
- $field : string
- $rules : array<string|int, mixed>
- $data : array<string|int, mixed>
-
The array of data to validate, with
DBGroup
.
Return values
array<string|int, mixed>|true —The modified rules or true if we return early
processPermitEmpty()
private
processPermitEmpty(array<string|int, mixed>|string $value, array<string|int, mixed> $rules, array<string|int, mixed> $data) : array<string|int, mixed>|true
Parameters
- $value : array<string|int, mixed>|string
- $rules : array<string|int, mixed>
- $data : array<string|int, mixed>
-
The array of data to validate, with
DBGroup
.
Return values
array<string|int, mixed>|true —The modified rules or true if we return early
retrievePlaceholders()
Retrieves valid placeholder fields.
private
retrievePlaceholders(string $rule, array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $rule : string
- $data : array<string|int, mixed>