-
Notifications
You must be signed in to change notification settings - Fork 1
Troubleshooting
Concrete symptoms and their fixes. For the bigger picture see The Validation Lifecycle.
The rule name is unknown. Causes:
-
A typo —
integerr,requierd,lenght. Fix the spelling. -
A custom rule used before it was registered — call
extend()beforevalidation(). -
A global function name — 2.0 no longer dispatches to global functions.
Wrap it:
$v->extend('ctype_digit', fn ($x) => ctype_digit((string) $x));
optional skips a field only when it is missing or null. An empty string
is present, so its rules run.
$v = new Validation(['nick' => '']);
$v->rule('nick', 'optional|alpha');
$v->validation(); // false — '' is present and not alphabeticGuard on the value first, or allow blanks explicitly. See Optional Fields.
That is by design — validation() consumes the queue each run. Re-queue any
rules you want re-checked. See
The Validation Lifecycle.
DSL arguments are split on commas, so an inline body like [a-z]{2,4} is broken
apart. Register it as a named pattern
instead:
$v->pattern('two_to_four', '[a-z]{2,4}');
$v->rule('code', 'regex(two_to_four)');Array membership is strict, and DSL arguments are always strings, so
in(2) looks for the string "2", not the integer 2.
$v = new Validation(['ids' => [1, 2, 3]]);
$v->rule('ids', 'in(2)'); // false — "2" !== 2For typed arrays, use a callback rule:
$v->rule('ids', static fn ($value): bool => in_array(2, (array) $value, true));Register a friendly label:
$v->labels(['user_email' => 'E-mail address']);See Error Messages.
The language file was not found. Check that the file exists at
<locale-dir>/<locale>.php and returns an array. Point at a custom directory
with setLocaleDir() before setLocale().
validation() clears the error list at the start of each run. Add manual
errors after validating, or in a flow that does not call validation() again.
See Error Messages.
For a numeric value, min/max compare the value; for a non-numeric string
they compare the length. A numeric string is treated as a number. If you
always want length, use length; for a value range use
range.
- FAQ — conceptual questions.
- Exceptions — what each exception means.
initphp/validation · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Rules
Extending
Messages
Reference
Guides
Other