Class: FieldValidator
Full name: Azera\Validation\FieldValidator
Fluent validator for a single input field.
Chain rules to describe what the field must look like.
The validator is executed by Validator
(or the nested model/list machinery)
via the internal validate() method.
Example:
$v->field('email')->required()->email()->max(255);
$v->field('age')->optional()->int()->min(18)->max(120);
$v->field('tags')->optional()->list(fn($f) => $f->string()->max(50));
Public methods
required() ·
public function required(): static
Return value
- Type:
static
optional() ·
public function optional(): static
Return value
- Type:
static
isRequired() ·
public function isRequired(): bool
Return value
- Type:
bool
default() ·
public function default(mixed $value): static
Supply a default value used when the field is absent.
Calling default() implicitly makes the field optional. The default is included in validated() as-is (no rules are applied to it).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$value |
mixed | - |
Return value
- Type:
static
hasDefault() ·
public function hasDefault(): bool
Return value
- Type:
bool
getDefault() ·
public function getDefault(): mixed
Return value
- Type:
mixed
int() ·
public function int(): static
Coerce to integer. Accepts int values and numeric strings (including negatives).
Return value
- Type:
static
float() ·
public function float(): static
Coerce to float. Accepts any numeric value.
Return value
- Type:
static
bool() ·
public function bool(): static
Coerce to bool. Accepts true/false, 1/0, "true"/"false", "yes"/"no", "on"/"off".
Return value
- Type:
static
string() ·
public function string(): static
Explicitly cast to string. Useful for ensuring min/max applies to character length.
Return value
- Type:
static
min() ·
public function min(int|float $n): static
Minimum value / length / count depending on type:
- string: minimum character length (mb_strlen)
- int/float: minimum numeric value
- array: minimum number of items
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$n |
int|float | - |
Return value
- Type:
static
max() ·
public function max(int|float $n): static
Maximum value / length / count (same semantics as min).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$n |
int|float | - |
Return value
- Type:
static
email() ·
public function email(): static
Value must be a valid e-mail address (RFC 5321).
Return value
- Type:
static
url() ·
public function url(): static
Value must be a valid URL (FILTER_VALIDATE_URL).
Return value
- Type:
static
ip() ·
public function ip(): static
Value must be a valid IPv4 or IPv6 address.
Return value
- Type:
static
pattern() ·
public function pattern(string $regex): static
Value must match the given regular expression.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$regex |
string | - |
Return value
- Type:
static
in() ·
public function in(array $allowed): static
Value must be strictly equal (===) to one of the allowed values.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$allowed |
array | - |
Return value
- Type:
static
domain() ·
public function domain(): static
Value must be a valid domain name (e.g. example.com), without scheme or path.
Return value
- Type:
static
custom() ·
public function custom(callable $fn): static
Custom validation callback. Return:
- null → valid, no error
- string → error with code 'custom' and the string as the message
- array → structured error; supports the same keys as built-in errors: 'code' (required) – error code passed to the translator 'params' (optional) – raw parameter values for placeholder replacement, default [] 'template' (optional) – English fallback template with {placeholder} markers; if omitted, looked up from the built-in TEMPLATES table or falls back to the code string itself
Multiple custom() calls are supported and stack; the first failure short-circuits.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$fn |
callable | - |
Return value
- Type:
static
list() ·
public function list(callable $configure): static
Value must be an array; each element is validated by the configured sub-validator.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$configure |
callable | - |
Return value
- Type:
static
model() ·
public function model(array $fields): static
Value must be an associative array matching the given field definitions.
Each entry maps a key name to a callable that configures a FieldValidator.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$fields |
array | - |
Return value
- Type:
static
validate() ·
public function validate(mixed $value, string $path, array &$errors): mixed
Apply all configured rules to $value, appending any errors to $errors.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$value |
mixed | - | The raw input value. |
$path |
string | - | Dot-path used as the error key. |
$errors |
array | - | Accumulated errors (mutated in place). |
Return value
- Type:
mixed - Description: The coerced / validated value.