A rule string is a pipe-separated list of rules. Arguments go in parentheses
and are comma-separated; each argument is trimmed, so only(a, b) equals
only(a,b). Rule names are matched case-insensitively (creditCard and
creditcard are the same rule).
All examples below assume:
use InitPHP\Validation\Validation;
$v = new Validation($data);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. When the field is present, the other rules apply.
$v->rule('nickname', 'optional|alpha');See Optional fields & patterns.
The value is empty once trimmed.
$v->rule('honeypot', 'empty');An integer or an integer-looking string ("-12", "12").
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).
An array.
$v->rule('id', 'integer');
$v->rule('price', 'float');
$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');
$v->rule('slug', 'alphanumeric');A valid e-mail address (FILTER_VALIDATE_EMAIL).
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).
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');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.
$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 element$v->rule('discount', 'max(100)');
$v->rule('bio', 'max(280)');A number within the range. Accepts min-max as well, and open bounds with
...max or min....
$v->rule('year', 'range(1970...2099)');
$v->rule('score', 'range(0-10)');
$v->rule('temp', 'range(-40...60)');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)');Matches a named pattern or an inline regex body (without delimiters).
$v->rule('email', 'regex(email)'); // built-in named pattern
$v->pattern('code', '[A-Z]{2}-[0-9]{4}');
$v->rule('product', 'regex(code)'); // custom named patternInline bodies are matched as
^(...)$. Avoid 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).
A date string in the given date() format.
$v->rule('published_at', 'dateFormat(Y/m/d)');A credit card number, ignoring spaces. With no argument, any supported brand
passes. With a type, only that brand passes: amex, visa, mastercard,
maestro, jcb, solo, switch.
$v->rule('card', 'creditCard');
$v->rule('card', 'creditCard(visa)');Loosely equals the value of another field in the data set — handy for password and e-mail confirmation.
$v->rule('confirm', 'again(password)');Loosely equals the given value.
$v->rule('agree', 'equals(1)');A string that starts/ends with the value. For arrays, compares the first/last element.
$v->rule('phone', 'startWith(+90)');
$v->rule('file', 'endWith(.pdf)');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 array
$v->rule('text', 'notIn(spam)');A case-sensitive substring test on the string form of the value.
$v->rule('text', 'contains(Dolor)');
$v->rule('text', 'notContains(http)');The value must equal one of the options. only is case-insensitive;
strictOnly is case-sensitive.
$v->rule('size', 'only(small, medium, large)');
$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)');
$v->rule('email|backup_email', 'optional|mail'); // both fields, same rulesContinue with custom rules & callables.