Validation

Implements \CodeIgniter\Validation\ValidationInterface

Validator

package

CodeIgniter

Methods

Validation constructor.

__construct(\Config\Validation $config, \CodeIgniter\View\RendererInterface $view) 

Arguments

$config

\Config\Validation

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

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

Arguments

$value

mixed

Value to validation.

$rule

string

Rule.

$errors

array<mixed,string>

Errors.

Response

boolean

True if valid, else false.

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:

fillPlaceholders(array $rules, array $data) : array

[ '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]'

Arguments

$rules

array

$data

array

Response

array

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

getError(string $field = null) : string

Arguments

$field

string

Field.

Response

string

Error(s).

Attempts to find the appropriate error message

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

Arguments

$rule

string

$field

string

$label

string|null

$param

string

$value

string

The value that caused the validation to fail.

Response

string

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

getErrors() : array

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

codeCoverageIgnore

Response

array

Excluded from code coverage because that it always run as cli

Get rule group.

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

If group not found.

Arguments

$group

string

Group.

Response

array<mixed,string>

Rule group.

Returns all of the rules currently defined.

getRules() : array

Response

array

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

hasError(string $field) : boolean

Arguments

$field

string

Response

boolean

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

hasRule(string $field) : boolean

Arguments

$field

string

Response

boolean

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

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

Arguments

$template

string

Response

string

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

loadRuleGroup(string|null $group = null) : array|\CodeIgniter\Validation\Exceptions\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.

Arguments

$group

string|null

Response

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

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

loadRuleSets() 

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.

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

Arguments

$field

string

$label

string|null

$value

string|array

Value to be validated, can be a string or an array

$rules

array|null

$data

array

// All of the fields to check.

Response

boolean

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

reset() : \CodeIgniter\Validation\ValidationInterface

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

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

Arguments

$data

array

The array of data to validate.

$group

string

The pre-defined group of rules to apply.

$db_group

string

The database group to use.

Response

boolean

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

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

Arguments

$field

string

$error

string

Response

\CodeIgniter\Validation\ValidationInterface

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

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

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

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

Arguments

$field

string

$label

string|null

$rules

string

$errors

array

Response

$this

Set rule group.

setRuleGroup(string $group) 
Throws
\InvalidArgumentException

If group not found.

Arguments

$group

string

Group.

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

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

Rules should be an array formatted like:

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

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

Arguments

$rules

array

$errors

array

// An array of custom error messages

Response

\CodeIgniter\Validation\ValidationInterface

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

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

Arguments

$field

string

$template

string

Response

string

Split rules string by pipe operator.

splitRules(string $rules) : array

Arguments

$rules

string

Response

array

Takes a Request object and grabs the input data to use from its array values.

withRequest(\CodeIgniter\HTTP\RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request) : \CodeIgniter\Validation\ValidationInterface

Properties

Files to load with validation functions.

ruleSetFiles : array
var

Type(s)

array

The loaded instances of our validation files.

ruleSetInstances : array
var

Type(s)

array

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

rules : array
var

Type(s)

array

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

data : array
var

Type(s)

array

Any generated errors during validation.

errors : array

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

var

Type(s)

array

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

customErrors : array
var

Type(s)

array

Our configuration.

config : \Config\Validation
var

Type(s)

\Config\Validation

The view renderer used to render validation messages.

view : \CodeIgniter\View\RendererInterface