Properties

$ruleSetFiles

$ruleSetFiles : array

Files to load with validation functions.

Type

array

$ruleSetInstances

$ruleSetInstances : array

The loaded instances of our validation files.

Type

array

$rules

$rules : array

Stores the actual rules that should be ran against $data.

Type

array

$data

$data : array

The data that should be validated, where 'key' is the alias, with value.

Type

array

$errors

$errors : array

Any generated errors during validation.

'key' is the alias, 'value' is the message.

Type

array

$customErrors

$customErrors : array

Stores custom error message to use during validation. Where 'key' is the alias.

Type

array

Methods

run()

run(array  $data = null, string  $group = null, string  $db_group = null) : boolean

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

Parameters

array $data

The array of data to validate.

string $group

The pre-defined group of rules to apply.

string $db_group

The database group to use.

Returns

boolean

check()

check(mixed  $value, string  $rule, array<mixed,string>  $errors = array()) : boolean

Check; runs the validation process, returning true or false determining whether validation was successful or not.

Parameters

mixed $value

Value to validation.

string $rule

Rule.

array<mixed,string> $errors

Errors.

Returns

boolean —

True if valid, else false.

setRule()

setRule(string  $field, string|null  $label = null, string  $rules, array  $errors = array()) : $this

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

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

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

Parameters

string $field
string|null $label
string $rules
array $errors

Returns

$this

setRules()

setRules(array  $rules, array  $errors = array()) : \CodeIgniter\Validation\ValidationInterface

Stores the rules that should be used to validate the items.

Rules should be an array formatted like:

[ 'field' => 'rule1|rule2' ]

The $errors array should be formatted like: [ 'field' => [ 'rule' => 'message', 'rule' => 'message ], ]

Parameters

array $rules
array $errors

// An array of custom error messages

Returns

\CodeIgniter\Validation\ValidationInterface

getRules()

getRules() : array

Returns all of the rules currently defined.

Returns

array

hasRule()

hasRule(string  $field) : boolean

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

Parameters

string $field

Returns

boolean

getRuleGroup()

getRuleGroup(string  $group) : array<mixed,string>

Get rule group.

Parameters

string $group

Group.

Throws

\InvalidArgumentException

If group not found.

Returns

array<mixed,string> —

Rule group.

setRuleGroup()

setRuleGroup(string  $group) 

Set rule group.

Parameters

string $group

Group.

Throws

\InvalidArgumentException

If group not found.

listErrors()

listErrors(string  $template = 'list') : string

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

Parameters

string $template

Returns

string

showError()

showError(string  $field, string  $template = 'single') : string

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

Parameters

string $field
string $template

Returns

string

loadRuleGroup()

loadRuleGroup(string|null  $group = null) : array|\CodeIgniter\Validation\Exceptions\ValidationException|null

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

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

string|null $group

Returns

array|\CodeIgniter\Validation\Exceptions\ValidationException|null

hasError()

hasError(string  $field) : boolean

Checks to see if an error exists for the given field.

Parameters

string $field

Returns

boolean

getError()

getError(string  $field = null) : string

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

Parameters

string $field

Field.

Returns

string —

Error(s).

getErrors()

getErrors() : array

Returns the array of errors that were encountered during a run() call. The array should be in the following format:

[ 'field1' => 'error message', 'field2' => 'error message', ]

Returns

array —

Excluded from code coverage because that it always run as cli

setError()

setError(string  $field, string  $error) : \CodeIgniter\Validation\ValidationInterface

Sets the error for a specific field. Used by custom validation methods.

Parameters

string $field
string $error

Returns

\CodeIgniter\Validation\ValidationInterface

reset()

reset() : \CodeIgniter\Validation\ValidationInterface

Resets the class to a blank slate. Should be called whenever you need to process more than one array.

Returns

\CodeIgniter\Validation\ValidationInterface

processRules()

processRules(string  $field, string|null  $label = null, string  $value, array|null  $rules = null, array  $data) : boolean

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.

Parameters

string $field
string|null $label
string $value
array|null $rules
array $data

// All of the fields to check.

Returns

boolean

loadRuleSets()

loadRuleSets() 

Loads all of the rulesets classes that have been defined in the Config\Validation and stores them locally so we can use them.

getErrorMessage()

getErrorMessage(string  $rule, string  $field, string|null  $label = null, string  $param = null) : string

Attempts to find the appropriate error message

Parameters

string $rule
string $field
string|null $label
string $param

Returns

string

splitRules()

splitRules(string  $rules) : array

Split rules string by pipe operator.

Parameters

string $rules

Returns

array