Esc
API Index — Namespaces, Packages, Reports, Indices
Validator
in package
FinalYes
Validation for component input. Rules are a |-separated string
('required|email|max:120') or a list mixing rule strings and closures
fn(mixed $value, string $field): ?string that return an error message or null.
Rules:
| Rule | Passes when |
|---|---|
required |
not null, not '', not [] (and false fails for booleans, like a checkbox that must be ticked: use accepted) |
email |
a valid email address |
url |
an http(s) URL |
min:n / max:n |
string length, number value, or array count is ≥ n / ≤ n |
between:a,b |
as min:a and max:b |
in:a,b,c / not_in:a,b,c |
the value (as a string) is / isn't in the list |
regex:/pattern/ |
matches; put it last or use the list form if the pattern contains | |
numeric, integer |
numeric / a whole number |
alpha, alpha_num, alpha_dash |
letters / letters+digits / plus - and _ |
accepted |
true, 1, '1', 'on', 'yes' |
Every rule except required and accepted skips empty values, so
'email' alone allows an empty field and 'required|email' doesn't.
Table of Contents
Methods
- label() : string
first_name/firstName→First name.- parse() : array<int, string|Closure>
- validate() : array<string, string>
- Validate each field and return the first error per failing field.
- check() : string|null
- defaultMessage() : string
- number() : float
- passes() : bool
- runClosure() : string|null
- size() : float
Methods
label()
first_name / firstName → First name.
public
static label(string $field) : string
Parameters
- $field : string
Return values
stringparse()
public
static parse(string|array<int, string|Closure> $rules) : array<int, string|Closure>
Parameters
- $rules : string|array<int, string|Closure>
Return values
array<int, string|Closure>validate()
Validate each field and return the first error per failing field.
public
static validate(array<string, mixed> $values, array<string, string|array<int, string|Closure>> $rules[, array<string, string> $messages = [] ]) : array<string, string>
Parameters
- $values : array<string, mixed>
- $rules : array<string, string|array<int, string|Closure>>
- $messages : array<string, string> = []
-
'field.rule' => 'message'or'rule' => 'message'
Return values
array<string, string>check()
private
static check(string $rule, mixed $value, string $field, array<string, string> $messages) : string|null
Parameters
- $rule : string
- $value : mixed
- $field : string
- $messages : array<string, string>
Return values
string|nulldefaultMessage()
private
static defaultMessage(string $name, string $arg, mixed $value) : string
Parameters
- $name : string
- $arg : string
- $value : mixed
Return values
stringnumber()
private
static number(string $arg, string $rule) : float
Parameters
- $arg : string
- $rule : string
Return values
floatpasses()
private
static passes(string $name, string $arg, mixed $value, string $field) : bool
Parameters
- $name : string
- $arg : string
- $value : mixed
- $field : string
Return values
boolrunClosure()
private
static runClosure(Closure $rule, mixed $value, string $field) : string|null
Parameters
- $rule : Closure
- $value : mixed
- $field : string
Return values
string|nullsize()
private
static size(mixed $value) : float
Parameters
- $value : mixed