Luminova Framework

Input Validation

Last updated: 2024-05-11 14:19:14

The Input Validation class in the Luminova Framework provides a robust mechanism for validating user-submitted data before processing or storing it in a database. With this class, you can define validation rules for each input field along with optional error messages, ensuring that the data meets your application's requirements and standards.

Validation Rules

You can set validation rules for input fields using a variety of predefined validation rules, including required fields, maximum and minimum lengths, alphanumeric characters, email addresses, integers, and more. Rules can be added individually or set from an array, allowing for flexible configuration. Additionally, custom validation rules can be defined using callback functions.

Defining Rules

Validation rules are specified by separating each rule with a pipe | and providing parameters in parentheses when needed. Rules can be applied to individual fields or set globally using associative arrays, with the option to specify custom error messages for each rule.

Error Message Formatting

Error messages can include placeholders such as [field], [value], and [rule] to provide dynamic information about the validation error. This allows for more informative and context-aware error messages, improving the user experience.



Properties

Define your validation rules.

public array $rules

Define your validation error messages per rule.

public array $messages

Methods

validate

Validate input data against the defined rules.

public validate(array<string,mixed> $input, array<int,mixed> $rules = []): bool

Parameters:

ParameterTypeDescription
$inputarray<string,mixed>Input data to validate.
$rulesarray<int,mixed>Optional array of rules to override default rules.

Return Value:

bool - Returns true if all rules are passed, otherwise false.


getErrors

Get validation error messages.

public getErrors(): array

Return Value:

array - Validation error messages.


getError

Get the error message for a specific field.

public getError(string $field): string

Parameters:

ParameterTypeDescription
$fieldstringName of the field.

Return Value:

string - Error message for the specified field.


getErrorField

Get the field causing the validation error.

public getErrorField(string $field): string

Parameters:

ParameterTypeDescription
$fieldstringName of the field.

Return Value:

string - Field causing the validation error.


setRules

Set validation rules with optional error messages.

public setRules(array&lt;int,string&gt; $rules, array&lt;string,mixed&gt; $messages = []): self

Parameters:

ParameterTypeDescription
$rulesarray<int,string>Validation rules.
$messagesarray<string,mixed>Optional error messages for validation rules.

Return Value:

ValidatorInterface - Instance of the ValidatorInterface.


addRule

Add a single rule with an optional error message.

public addRule(string $field, string $rules, array&lt;string,string&gt; $messages = []): self

Parameters:

ParameterTypeDescription
$fieldstringField name.
$rulesstringValidation rules.
$messagesarray<string,string>Optional error message for the validation rule.

Return Value:

ValidatorInterface - Instance of the ValidatorInterface.


getErrorLine

Get the error message at the specified field and error indexes.

public getErrorLine(int $indexField, int $indexErrors): string

Parameters:

ParameterTypeDescription
$indexFieldintField index.
$indexErrorsintError index.

Return Value:

string - Error message.


getErrorLineInfo

Get information about the validation error at the specified field index.

public getErrorLineInfo(int $fieldIndex): array

Parameters:

ParameterTypeDescription
$fieldIndexintField index.

Return Value:

array - Information about the validation error.


getErrorFieldLine

Get the field causing the current validation error.

public getErrorFieldLine(string $prefix = ''): string

Parameters:

ParameterTypeDescription
$prefixstringPrefix to prepend to the error field.

Return Value:

string - Field causing the validation error.


isPassed

Check if validation has passed.

public isPassed(): bool

Return Value:

bool - True if all rules passed, false otherwise.


Validation Rules

RuleParameterDescription
noneVoidIgnore filed and return true
requiredVoidField is required, it cannot be empty
max_length()IntegerField maximum allowed length alias max()
min_length()IntegerField minimum allowed length alias min()
alphanumericVoidField should must be only alphanumeric [aZ-Az-0-9]
emailVoidField should must be a valid email address
integer()StringField should must an integer value, optional parameter [positive or negative] [-0-9]
equals()StringField value must match with anther specified field value
urlVoidField must be a valid URL
alphabetVoidField must be only an alphabet [aZ-Az]
uuid()IntegerField value must be uuid string, optional parameter for uuid version
exact_length()IntegerField value must be exact length alias length()
in_array()StringField value must be in array list
is_listVoidFiled value must be a valid string list
keys_exist()StringField array values must match in validation list
callback()CallableCallback myFunction(value, field) return boolean value
ip()IntegerField value must be a valid IP address, optional parameter for ip version. e.g ip(6). Pass 0 to validate ipv4 or ipv6.
decimalVoidField must be a valid decimal value
match()RegexField value must match the regular expression [/pattern/]
fallback()MixedIf the field value is empty, replace it with the default value, if parameter is empty any empty string will be used instead
phoneVoidCheck if field value is a phone number
binaryVoidCheck if field value is a binary
hexadecimalVoidCheck if field value is a hexadecimal
arrayVoidCheck if field value is a array
jsonVoidCheck if field value is a JSON object
path()StringCheck if field value is directory path, if parameter [true] is specified it will check if path is readable
scheme()StringCheck if field value URL scheme matched with the specified URL scheme.

Defining your own custom rule using the callback rule method, your callback must be callable and must return true or false to indicate validation failure or passed.

Additionally the callback method should be design to accept 2 parameters value and field as second parameter.