Documentation

Validation
in package
implements ValidationInterface

Validator

Interfaces, Classes and Traits

ValidationInterface
Expected behavior of a validator

Table of Contents

$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, mixed>
Stores the actual rules that should be ran 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.
$view  : RendererInterface
The view renderer used to render validation messages.
__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<string|int, string>
Get rule group.
getRules()  : array<string|int, mixed>
Returns all of the rules currently defined.
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<string|int, mixed>|ValidationException|null
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 an individual rule and custom error messages for a single field.
setRuleGroup()  : mixed
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()  : mixed
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.

Properties

$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 ran against $data.

protected array<string|int, mixed> $rules = []

$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 = []

Methods

check()

Runs the validation process, returning true or false determining whether validation was successful or not.

public check(mixed $value, string $rule[, array<string|int, string> $errors = [] ]) : bool
Parameters
$value : mixed
$rule : string
$errors : array<string|int, string> = []
Return values
bool

getError()

Returns the error(s) for a specified $field (or empty string if not set).

public getError([string $field = null ]) : string
Parameters
$field : string = null

Field.

Return values
string

Error(s).

getErrors()

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
codeCoverageIgnore
Return values
array<string, string>

Excluded from code coverage because that it always run as cli

getRuleGroup()

Get rule group.

public getRuleGroup(string $group) : array<string|int, string>
Parameters
$group : string

Group.

Tags
throws
InvalidArgumentException

If group not found.

Return values
array<string|int, string>

Rule group.

getRules()

Returns all of the rules currently defined.

public getRules() : 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
bool

hasRule()

Checks to see if the rule for key $field has been set or not.

public hasRule(string $field) : bool
Parameters
$field : string
Return values
bool

listErrors()

Returns the rendered HTML of the errors as defined in $template.

public listErrors([string $template = 'list' ]) : string
Parameters
$template : string = 'list'
Return values
string

loadRuleGroup()

Loads custom rule groups (if set) into the current rules.

public loadRuleGroup([string|null $group = null ]) : array<string|int, mixed>|ValidationException|null

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 : string|null = null
Return values
array<string|int, mixed>|ValidationException|null

run()

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 ][, 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 : string|null = null

The database group to use.

Return values
bool

setRule()

Sets an individual rule and custom error messages for a single field.

public setRule(string $field[, string|null $label = null ], string $rules[, array<string|int, mixed> $errors = [] ]) : $this

The custom error message should be just the messages that apply to this field, like so:

[ 'rule' => 'message', 'rule' => 'message' ]

Parameters
$field : string
$label : string|null = null
$rules : string
$errors : array<string|int, mixed> = []
Return values
$this

setRuleGroup()

Set rule group.

public setRuleGroup(string $group) : mixed
Parameters
$group : string

Group.

Tags
throws
InvalidArgumentException

If group not found.

Return values
mixed

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' => [ 'rule' => 'message', 'rule' => 'message ], ]

Parameters
$rules : array<string|int, mixed>
$errors : array<string|int, mixed> = []

// An array of custom error messages

Return values
ValidationInterface

showError()

Displays a single error in formatted HTML as defined in the $template view.

public showError(string $field[, string $template = 'single' ]) : string
Parameters
$field : string
$template : string = 'single'
Return values
string

fillPlaceholders()

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:

'required|is_unique[users,email,id,{id}]'

The value of {id} would be replaced with the actual id in the form data:

'required|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[, string|null $label = null ][, string $param = null ][, string $value = null ]) : string
Parameters
$rule : string
$field : string
$label : string|null = null
$param : string = null
$value : string = null

The value that caused the validation to fail.

Return values
string

loadRuleSets()

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() : mixed
Return values
mixed

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 = null ], string|array<string|int, mixed> $value[, array<string|int, mixed>|null $rules = null ][, array<string|int, mixed> $data = null ]) : bool
Parameters
$field : string
$label : string|null = null
$value : string|array<string|int, mixed>
$rules : array<string|int, mixed>|null = null
$data : array<string|int, mixed> = null
Return values
bool

splitRules()

Split rules string by pipe operator.

protected splitRules(string $rules) : array<string|int, mixed>
Parameters
$rules : string
Return values
array<string|int, mixed>

Search results