Rules Reference
Cast Rules
| Rule |
Description |
Example |
boolean, bool |
Convert value to boolean. Only clear values are accepted: true, 1, yes, on and false, 0, no, off, '', null (text without distinguishing case); any other value is an error |
'cast' => 'boolean' |
date |
Convert an absolute ISO 8601 date to a Carbon date (see Absolute dates) |
'cast' => 'date' |
datetime |
Convert an absolute ISO 8601 date and time to a Carbon datetime (see Absolute dates) |
'cast' => 'datetime' |
float, decimal |
Convert value to float. A float does not keep trailing zeros ('10.50' is 10.5), so validate its decimals with decimal:0,N (up to N decimals), not with decimal:N |
'cast' => 'float' |
integer, int |
Convert value to integer. A number with a decimal part ('1.9') or out of range is an error, it is not truncated; '1.0' and '1e2' are 1 and 100 |
'cast' => 'integer' |
string, str |
Convert value to string |
'cast' => 'string' |
timestamp |
Convert value to Unix timestamp. A number is used as it is; a text must be an absolute ISO 8601 date (see Absolute dates) |
'cast' => 'timestamp' |
Transform Rules
| Rule |
Description |
Example |
base64_decode |
Decode base64 string |
'transform' => ['base64_decode'] |
base64_encode |
Encode value to base64 |
'transform' => ['base64_encode'] |
json_decode |
Decode JSON string |
'transform' => ['json_decode'], 'transform' => ['json_decode:array'] |
json_encode |
Encode value to JSON |
'transform' => ['json_encode'], 'transform' => ['json_encode:pretty'] |
json_to_array |
Convert JSON string to array |
'transform' => ['json_to_array'] |
json_to_object |
Convert JSON string to object |
'transform' => ['json_to_object'] |
round |
Round numeric value |
'transform' => ['round:2'] |
lowercase, lower |
Convert string to lowercase |
'transform' => ['lowercase'] |
slug |
Convert string to URL friendly format |
'transform' => ['slug'] |
transliterate |
Convert characters to ASCII |
'transform' => ['transliterate'] |
uppercase, upper |
Convert string to uppercase |
'transform' => ['uppercase'] |
Sanitize Rules
| Rule |
Description |
Example |
addslashes |
Add slashes before quotes |
'sanitize' => ['addslashes'] |
htmlentities |
Convert characters to HTML entities |
'sanitize' => ['htmlentities'] |
htmlspecialchars |
Convert special characters to HTML entities |
'sanitize' => ['htmlspecialchars'] |
regex_keep |
Keep only characters matching pattern |
'sanitize' => ['regex_keep:/[0-9]/'] |
regex_remove |
Remove characters matching pattern |
'sanitize' => ['regex_remove:/[^a-z]/'] |
remove_chars |
Remove specific characters |
'sanitize' => ['remove_chars:@#$'] |
remove_non_printable |
Remove ASCII control characters (except tab and line feed), C1 control characters and invisible format characters (U+200B, U+200E, U+202A to U+202E, U+2060, U+FEFF, U+00AD). U+200C and U+200D are kept, because some scripts and emoji sequences need them |
'sanitize' => ['remove_non_printable'] |
remove_prefix |
Remove string prefix |
'sanitize' => ['remove_prefix:http'] |
remove_suffix |
Remove string suffix |
'sanitize' => ['remove_suffix:.txt'] |
spaces |
Normalize multiple spaces to single |
'sanitize' => ['spaces'] |
strip_tags |
Remove HTML and PHP tags |
'sanitize' => ['strip_tags'], 'sanitize' => ['strip_tags:<p><br>'] |
substring |
Extract part of string |
'sanitize' => ['substring:5'], 'sanitize' => ['substring:0,10'] |
trim |
Remove ASCII whitespace from ends (not the no-break space U+00A0) |
'sanitize' => ['trim'] |
Validate Rules
Array Validation
| Rule |
Description |
Example |
allowed_keys |
Check that an array has only the listed keys (additionalProperties: false of a JSON Schema object) |
'validate' => ['allowed_keys:name,email'] |
contains |
Check that the array has at least one of the listed values |
'validate' => ['contains:terms'], 'validate' => ['contains:a,b'] |
dependent_required |
If the first key has a value, the other keys must have a value too |
'validate' => ['dependent_required:card,address,zip'] |
in, choices |
Check if value is in list. Value and list are compared as text (2, 2.0 and '2' are the same choice for in:1,2,3); an array is valid when each element is in the list; booleans, null and objects are never in the list |
'validate' => ['in:a,b,c'], 'validate' => ['in:1,2,3'] |
max_contains |
Check that the array has at most N elements that are one of the listed values |
'validate' => ['max_contains:1,a,b'] |
max_items |
Check array maximum length. In an associative array, it counts the keys |
'validate' => ['max_items:5'] |
min_contains |
Check that the array has at least N elements that are one of the listed values |
'validate' => ['min_contains:2,a,b'] |
min_items |
Check array minimum length. In an associative array, it counts the keys |
'validate' => ['min_items:1'] |
notempty |
Check if array is not empty |
'validate' => ['notempty'] |
unique |
Check if array values are unique. The comparison is strict (same type and value): ['1e1', '10'] and ['1', 1] are unique |
'validate' => ['unique'] |
unique_loose |
Check if array values are unique with a loose comparison (==): ['1e1', '10'] and ['1', 1] are duplicates |
'validate' => ['unique_loose'] |
Date Validation
| Rule |
Description |
Example |
after |
Check if date is after another |
'validate' => ['after:2024-01-01'] |
after_or_equal |
Check if date is after or equal |
'validate' => ['after_or_equal:2024-01-01'] |
before |
Check if date is before another |
'validate' => ['before:2024-12-31'] |
before_or_equal |
Check if date is before or equal |
'validate' => ['before_or_equal:2024-12-31'] |
between |
Check if date is between two dates |
'validate' => ['between:2024-01-01,2024-12-31'] |
date_equals |
Check if date equals another |
'validate' => ['date_equals:2024-01-01'] |
date_format |
Check if date matches format |
'validate' => ['date_format:Y-m-d'] |
datetime_local |
Check a date and time as <input type="datetime-local"> sends it: YYYY-MM-DDTHH:MM or YYYY-MM-DDTHH:MM:SS, with an existing date |
'validate' => ['datetime_local'] |
month |
Check a month as <input type="month"> sends it: YYYY-MM |
'validate' => ['month'] |
time |
Check a time as <input type="time"> sends it: HH:MM or HH:MM:SS |
'validate' => ['time'] |
week |
Check a week as <input type="week"> sends it: YYYY-Www, with the week 53 only in the years that have it |
'validate' => ['week'] |
weekday |
Check if date is weekday |
'validate' => ['weekday'] |
weekend |
Check if date is weekend |
'validate' => ['weekend'] |
File Validation
| Rule |
Description |
Example |
file |
Validate file with size limits (max_size, and min_size, 1 byte by default) |
'validate' => ['file:2M'], 'validate' => ['file:max_size=2M,min_size=0'] |
image |
Validate image file (JPEG, PNG, GIF or WebP) with size limits (max_size, and min_size, 1 byte by default) |
'validate' => ['image:2M'], 'validate' => ['image:max_size=2M,min_size=0'] |
mimetype |
Validate file mime type, detected from its content. A whole type allows any of its subtypes with type/*; any other * is text and never matches |
'validate' => ['mimetype:application/pdf,image/jpeg'], 'validate' => ['mimetype:image/*'], 'validate' => ['mimetype:application/pdf,application/x-empty'] |
Financial Validation
| Rule |
Description |
Example |
bic |
Validate BIC/SWIFT code (it identifies a bank, not an account) |
'validate' => ['bic'] |
card_number |
Validate credit card number |
'validate' => ['card_number'] |
iban |
Validate IBAN: country, length of that country, and check digits. Spaces and lowercase are accepted. Only the countries of the SWIFT IBAN Registry have IBAN (in Latin America: Brazil, Costa Rica, Dominican Republic, Guatemala, Honduras, Nicaragua and El Salvador), any other country is an error |
'validate' => ['iban'] |
I18n Validation
| Rule |
Description |
Example |
country |
Validate country code |
'validate' => ['country'] |
currency |
Validate ISO 4217 currency code, in uppercase or lowercase. It has the codes in force, with the funds codes (CLF), and not the precious metals and special codes (XAU, XAG, XDR, XXX, etc.) |
'validate' => ['currency'] |
language |
Validate language code (2 or 3 letters), with an optional country: es, fil, es_CL or es-CL |
'validate' => ['language'] |
locale |
Validate locale code, with _ or - as separator: es_CL or es-CL. The list is the one of ICU, so it depends on the version of ICU |
'validate' => ['locale'] |
timezone |
Validate timezone identifier |
'validate' => ['timezone'] |
ID Validation
| Rule |
Description |
Example |
uuid |
Validate UUID string (RFC 9562): versions 1 to 8, and the nil and max UUIDs. With a version, only that version is valid |
'validate' => ['uuid'], 'validate' => ['uuid:4'] |
Internet Validation
| Rule |
Description |
Example |
email |
Validate email address. The domain can have international characters (jose@exämple.com); the part before @ must be ASCII |
'validate' => ['email'] |
host |
Validate a hostname or an IP address. host:v4 and host:v6 limit the version of the IP address; a hostname is valid anyway |
'validate' => ['host'], 'validate' => ['host:v4'] |
hostname |
Validate hostname. The last label can not be only digits, so 1.2.3.4 and example.123 are not hostnames (RFC 1123) |
'validate' => ['hostname'] |
ip |
Validate IP address |
'validate' => ['ip'], 'validate' => ['ip:v4'], 'validate' => ['ip:v6'] |
url |
Validate URL. With parameters, only those schemes are valid. A host with international characters is accepted |
'validate' => ['url'], 'validate' => ['url:http,https'] |
Numeric Validation
| Rule |
Description |
Example |
decimal, float, real |
Validate decimal number with exactly N decimals (decimal:2), or between min and max decimals (decimal:0,2) |
'validate' => ['decimal:2'], 'validate' => ['decimal:0,2'] |
digits_range |
Validate that the value is only digits, with a number of digits between min and max |
'validate' => ['digits_range:3,5'] |
digits |
Validate that the value is only digits, with exactly that number of digits |
'validate' => ['digits:4'] |
gt |
Greater than |
'validate' => ['gt:10'] |
gte, min |
Greater than or equal |
'validate' => ['gte:10'] |
integer, int |
Validate integer |
'validate' => ['integer'] |
lt |
Less than |
'validate' => ['lt:10'] |
lte, max |
Less than or equal |
'validate' => ['lte:10'] |
multiple_of |
Check if number is multiple |
'validate' => ['multiple_of:5'] |
numeric |
Validate numeric value |
'validate' => ['numeric'] |
range |
Validate number in range |
'validate' => ['range:1,100'] |
String Validation
| Rule |
Description |
Example |
alpha |
Only alphabetic characters |
'validate' => ['alpha'] |
alpha_dash |
Alphanumeric with dashes/underscores |
'validate' => ['alpha_dash'] |
alpha_num |
Only alphanumeric characters |
'validate' => ['alpha_num'] |
base64 |
Validate base64 string |
'validate' => ['base64'] |
color |
Check a color as <input type="color"> sends it: #rrggbb, in uppercase or lowercase (#fff and red are not valid) |
'validate' => ['color'] |
ends_with |
String ends with value |
'validate' => ['ends_with:Test'] |
json |
Validate JSON string |
'validate' => ['json'] |
length |
Exact string length |
'validate' => ['length:10'] |
max_length |
Maximum string length |
'validate' => ['max_length:255'] |
min_length |
Minimum string length |
'validate' => ['min_length:3'] |
not_regex |
Not match regular expression |
'validate' => ['not_regex:/[0-9]/'] |
no_whitespace |
No whitespace in string |
'validate' => ['no_whitespace'] |
regex |
Match regular expression |
'validate' => ['regex:/^[A-Z]+$/'] |
required |
Value is required (null, '' and [] fail) |
'validate' => ['required'] |
slug |
Validate URL friendly string |
'validate' => ['slug'] |
starts_with |
String starts with value |
'validate' => ['starts_with:Test'] |
Notes
Rule parameters
The parameters of a rule go after :, separated by commas. A comma that is part of a parameter is written as \,: 'in:a\,b,c' has two choices, a,b and c. A parameter that is not valid is an error, it is not replaced by another value ('uuid:9' fails with Invalid parameter: 9).
The rules that take a literal (regex, not_regex, starts_with, ends_with, date_format, after, before, trim, remove_chars, strip_tags, transliterate, etc.) receive all the text after : as one parameter, and \, has no special meaning for them.
Empty values
A value that is null, '' or [] has nothing to cast, transform or sanitize. If the rules do not have required, the result is null and the validators do not run, so an optional field sent empty is valid. If the rules have required, the validators run and the value fails with Field is required.
Absolute dates
The date validators (after, before, between, date_equals, weekday, weekend, etc.) and the casters date, datetime and timestamp accept as text only an absolute date in ISO 8601: YYYY-MM-DD, or with time, YYYY-MM-DDTHH:MM[:SS[.fraction]] (a space can replace the T), with Z or an offset (+03:00) at the end. The date and the time must exist. Date objects are accepted too.
A relative text (tomorrow, +1 week, now) or another format (01/02/2024, which can be read as January 2 or February 1) is an error, because its value depends on the clock of the server or on how it is read. date_format validates a text in another format; to convert it, read it with your own code before processing. The parameter that a rule compares with (after:tomorrow) can still be relative.
Arrays and objects
The array validators also work with an associative array (an object): min_items and max_items count its keys.
contains, min_contains and max_contains compare the elements with the listed values as text, like in: 2, 2.0 and '2' are the same value. Booleans, null and arrays are never equal to a value. In min_contains and max_contains the first parameter is the number N, and the rest are the values: 'min_contains:2,a,b' needs at least two elements that are a or b. N must be an integer of 0 or more.
allowed_keys compares the keys of the array as text. 'allowed_keys:a,b' fails with Properties not allowed: c. for ['a' => 1, 'c' => 2].
In dependent_required the first parameter is the key that triggers the rule, and the rest are the keys that become required. A key has a value when it exists and is not null, '' or [] (false and 0 are values). If the first key has no value, the rule passes. 'dependent_required:card,address' fails with Properties required when card is present: address. for ['card' => '4111', 'address' => ''].
A value that is not an array fails with Value must be an array.