Validator
in package
Class Validator
A utility class to validate associative arrays against field rules. Supports rule strings (e.g., "required|email|min:18") or callables for custom logic. Returns an array of error messages per field — does not throw exceptions.
Performs validation on form or data arrays using string-based rule syntax or callables. String rules are separated by pipe (|), and support parameters via colon (e.g., min:18).
Tags
Table of Contents
Methods
- validate() : array<string, array<string|int, string>>
- Validates data against defined rules.
- ruleBoolean() : string|null
- Checks if the value is boolean (or a 0/1 string or integer).
- ruleEmail() : string|null
- Checks if the value is a valid email.
- ruleExists() : string|null
- Checks that a value exists in a given table/column.
- ruleIn() : string|null
- Checks if the value is within a set of allowed values.
- ruleMax() : string|null
- Ensures the value does not exceed the given maximum.
- ruleMin() : string|null
- Ensures the value is at least the given minimum.
- ruleNumeric() : string|null
- Checks if the value is numeric.
- ruleRegex() : string|null
- Validates the value against a regex pattern.
- ruleRequired() : string|null
- Checks if the value is non-empty.
- ruleUnique() : string|null
- Validates that the value is unique in a database table/column.
Methods
validate()
Validates data against defined rules.
public
static validate(array<string|int, mixed> $data, array<string|int, mixed> $rules) : array<string, array<string|int, string>>
Parameters
- $data : array<string|int, mixed>
-
Associative array of data to validate (field => value).
- $rules : array<string|int, mixed>
-
Associative array of rules (field => string rule or callable).
Return values
array<string, array<string|int, string>> —An array of error messages indexed by field.
ruleBoolean()
Checks if the value is boolean (or a 0/1 string or integer).
protected
static ruleBoolean(mixed $value) : string|null
Parameters
- $value : mixed
Return values
string|nullruleEmail()
Checks if the value is a valid email.
protected
static ruleEmail(mixed $value) : string|null
Parameters
- $value : mixed
Return values
string|nullruleExists()
Checks that a value exists in a given table/column.
protected
static ruleExists(mixed $value, string|null $param) : string|null
Parameters
- $value : mixed
- $param : string|null
-
Format: "table,column"
Tags
Return values
string|nullruleIn()
Checks if the value is within a set of allowed values.
protected
static ruleIn(mixed $value, string|null $param) : string|null
Parameters
- $value : mixed
- $param : string|null
-
Comma-separated list of allowed values.
Return values
string|nullruleMax()
Ensures the value does not exceed the given maximum.
protected
static ruleMax(mixed $value, string|null $param) : string|null
Parameters
- $value : mixed
- $param : string|null
Return values
string|nullruleMin()
Ensures the value is at least the given minimum.
protected
static ruleMin(mixed $value, string|null $param) : string|null
Parameters
- $value : mixed
- $param : string|null
Return values
string|nullruleNumeric()
Checks if the value is numeric.
protected
static ruleNumeric(mixed $value) : string|null
Parameters
- $value : mixed
Return values
string|nullruleRegex()
Validates the value against a regex pattern.
protected
static ruleRegex(mixed $value, string|null $param) : string|null
Parameters
- $value : mixed
- $param : string|null
-
Regular expression.
Return values
string|nullruleRequired()
Checks if the value is non-empty.
protected
static ruleRequired(mixed $value) : string|null
Parameters
- $value : mixed
Return values
string|nullruleUnique()
Validates that the value is unique in a database table/column.
protected
static ruleUnique(mixed $value, string|null $param) : string|null
Parameters
- $value : mixed
- $param : string|null
-
Format: "table,column"