-
Notifications
You must be signed in to change notification settings - Fork 1
Rules Reference
Every built-in rule, grouped by purpose. Rules are written in a pipe-separated
DSL string; arguments go in parentheses and are comma-separated and trimmed
(only(a, b) equals only(a,b)). Rule names are matched case-insensitively
(creditCard and creditcard are the same rule).
All examples assume:
use InitPHP\Validation\Validation;
$v = new Validation($data);Need something custom? See Callable & Custom Rules.
-
Presence:
required·optional·empty -
Types:
integer·float·numeric·string·boolean·array -
Text:
alpha·alphanum/alphanumeric -
Contact & network:
mail·mailHost·url·urlHost·ip/ipv4/ipv6 -
Size & length:
min·max·range·length -
Pattern & format:
regex·date·dateFormat·creditCard -
Comparison:
again·equals·startWith·endWith·in·notIn·contains·notContains·only·strictOnly
The value is present and not a blank string. Numbers (including 0) and
non-empty arrays pass; null, '', whitespace-only strings and empty arrays
fail.
$v->rule('name', 'required');A pseudo-rule, not a check. When the field has no value, its other rules are skipped instead of failing. See Optional Fields.
$v->rule('nickname', 'optional|alpha');The value is empty once trimmed.
$v->rule('honeypot', 'empty'); // passes for "", " "An integer or an integer-looking string ("-12", "12"). Floats and
non-numeric strings fail.
A float or int, or a float-looking string ("13.50", "-2.00"). Every integer
is also a valid float.
Any numeric value: ints, floats and numeric strings.
A real string — this checks the type, so 12 (int) fails but "12" passes.
A real boolean, or one of true, false, 1, 0 (as int or string). Other
integers such as 2 fail.
An array.
$v->rule('id', 'integer');
$v->rule('price', 'float');
$v->rule('active', 'boolean');
$v->rule('tags', 'array');Letters only, Unicode-aware (\p{L}+). Spaces and digits fail.
Letters and digits only. The two names are aliases.
$v->rule('first_name', 'alpha'); // "José" passes, "John Doe" fails
$v->rule('slug', 'alphanumeric'); // "abc123" passes, "abc 123" failsA valid e-mail address (FILTER_VALIDATE_EMAIL).
mailHost(host, ...) — a valid e-mail whose host is one of the given hosts.
$v->rule('email', 'mailHost(gmail.com, outlook.com)');A valid URL (FILTER_VALIDATE_URL).
urlHost(domain, ...) — a URL whose host equals one of the domains, or is a
subdomain of it.
$v->rule('site', 'urlHost(example.com)');
// passes for http://example.com and http://www.example.comA valid IP address of the requested family.
$v->rule('remote', 'ip');
$v->rule('gateway', 'ipv4');
$v->rule('client', 'ipv6');For min/max, numbers are compared by value and strings/arrays by their
length/element count. range is value-only; length is length/count-only.
min(n):
$v->rule('age', 'min(18)'); // age >= 18 (number)
$v->rule('title', 'min(3)'); // at least 3 characters
$v->rule('items', 'min(1)'); // at least 1 elementmax(n):
$v->rule('discount', 'max(100)');
$v->rule('bio', 'max(280)');range(min...max) — a number within the range. Also accepts min-max, and open
bounds ...max or min.... Negative bounds work with the ... separator.
$v->rule('year', 'range(1970...2099)');
$v->rule('score', 'range(0-10)');
$v->rule('temp', 'range(-40...60)');length(spec) — string length or array element count. spec may be:
- a single number — treated as the maximum (
length(255)), - a closed range —
length(3...20)orlength(3-20), - an open range —
length(...255)(max only) orlength(10...)(min only).
$v->rule('username', 'length(3...20)');
$v->rule('comment', 'length(...500)');
$v->rule('password', 'length(8...)');regex(name | body) — matches a named pattern or an
inline regex body (without delimiters). The value is matched anchored as
^(...)$.
$v->rule('email', 'regex(email)'); // built-in named pattern
$v->pattern('code', '[A-Z]{2}-[0-9]{4}');
$v->rule('product', 'regex(code)'); // your own named patternAvoid commas inside an inline body — they are parsed as argument separators. Register a named pattern instead.
A DateTimeInterface instance, or any string strtotime() can parse
(2022-01-01, 10 September 2000, next Thursday).
$v->rule('published_at', 'date');dateFormat(format) — a date string in the given date() format.
$v->rule('published_at', 'dateFormat(Y/m/d)'); // "2022/01/01" passescreditCard(type?) — a credit card number, ignoring spaces. With no argument any
supported brand passes; with a type, only that brand: amex, visa,
mastercard, maestro, jcb, solo, switch.
$v->rule('card', 'creditCard');
$v->rule('card', 'creditCard(visa)');again(field) — loosely equals the value of another field in the data set.
Ideal for password and e-mail confirmation.
$v->rule('confirm', 'again(password)');equals(value) — loosely equals the given value, so equals(123) matches both
123 and "123".
$v->rule('agree', 'equals(1)');startWith(value) — a string that starts with the value, or an array whose
first element equals it.
$v->rule('phone', 'startWith(+90)');endWith(value) — a string that ends with the value, or an array whose last
element equals it.
$v->rule('file', 'endWith(.pdf)');in(needle) — for strings/numbers: a case-insensitive substring test. For
arrays: strict membership.
$v->rule('text', 'in(dolor)'); // 'dolor' appears in the string
$v->rule('roles', 'in(admin)'); // 'admin' is an element of the arraynotIn(needle) — the inverse of in.
$v->rule('text', 'notIn(spam)');contains(needle) — a case-sensitive substring test on the string form of
the value.
$v->rule('text', 'contains(Dolor)');notContains(needle) — the inverse of contains.
$v->rule('text', 'notContains(http)');only(a, b, ...) — the value case-insensitively equals one of the options.
$v->rule('size', 'only(small, medium, large)'); // "Medium" passesstrictOnly(a, b, ...) — the value equals one of the options, case-sensitive.
$v->rule('flag', 'strictOnly(Y, N)');Pipe rules together; they run left to right and each failure adds a message.
$v->rule('username', 'required|alphanum|length(3...20)');
// the same rules for two fields at once
$v->rule('email|backup_email', 'optional|mail');-
Optional Fields — the
optionalpseudo-rule in depth. - Callable & Custom Rules — when the built-ins are not enough.
- Error Messages — the message each rule produces.
initphp/validation · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Rules
Extending
Messages
Reference
Guides
Other