Validator Rules
This is the list of all possible validator rules with examples.
alpha
Verifies that value contains characters from the current locale by using ctype_alpha.
$validator = Validator::create([
'param' => 'alpha'
]);
alphaNum
Works same as alpha, except it allows numbers as well.
$validator = Validator::create([
'param' => 'alphaNum'
]);
anyOf
Verifies that value is one of the possible values.
$validator = Validator::create([
'param' => 'anyOf:one,two,3,four'
]);
If possible value contains comma, then urlencode the rule:
$validator = Validator::create([
'param' => 'anyOf:one,two%2C maybe three'
]);
array
Verifies that value is array.
$validator = Validator::create([
'param' => 'array'
]);
If you know how many elements array can have (for example, you want array to have 5 elements), then you can define it like:
$validator = Validator::create([
'param' => 'array:5'
]);
bool, boolean
Verifies that value is type of boolean
. Both rules (bool
and boolean
) works the same.
$validator = Validator::create([
'param' => 'bool',
'param2' => 'boolean'
]);
csrf
This rule is automatically used by framework when CSRF check is enabled in mandatory configuration, although you can use it yourself as well if you know how to use it.
$validator = Validator::create([
'param' => 'csrf'
]);
date
Verifies that value is in the requested format or in some general format recognizable by PHP.
$validator = Validator::create([
'param' => 'date',
'param2' => 'date:Y-m-d'
]);
In the example above, param
can be anything that strtotime()
can recognize. If you define format, then framework
will try to construct date with DateTime::createFromFormat()
and if it fails, validator will report an error.
Possible formats can be found in offical PHP documentation.
decimal
Verifies that value is numeric with decimal places. Number of decimals must be defined.
$validator = Validator::create([
'param' => 'decimal:2'
]);
different
Verifies that parameter doesn't have the same value as other parameter in the same data set.
$validator = Validator::create([
'param1' => null,
'param2' => 'different:param'
]);
Verifies that value is email in the correct format.
$validator = Validator::create([
'param' => 'email'
]);
endsWith
Verifies that value ends with the string from parameter:
$validator = Validator::create([
'phone' => 'endsWith:00', // phone parameter must end with "00"
]);
exists
This validator checks if given value exists in targeted table in database. It'll perform simple select query using database model.
For example, let's say you have class \Db\User
that extends \Koldy\Db\Model
and it's using databaes table
name user
. You want to check if there's already a record under id
column with the given value:
$validator = Validator::create([
'user_id' => 'exists:\Db\User,id'
]);
If your user_id
is, for example, 5
, framework will search for record in user
table where id=5
. If there is
a record present in database, rule will say this is correct, otherwise it'll return an error saying record is missing.
hex
Verifies that value contains only hexadecimal characters using ctype_xdigit.
$validator = Validator::create([
'param' => 'hex'
]);
integer
Verifies that given value is integer. It allows negative integers as well by ignoring the -
sign.
$validator = Validator::create([
'param' => 'integer'
]);
is
Verifies that value has the value requested in rule, literally meaning: This value is:
$validator = Validator::create([
'param' => 'is:yes'
]);
This is useful for sign up forms where user must agree to terms and conditions. If checkbox for agreement is named agreement and its check value is agreed, you would then validate like:
$validator = Validator::create([
'agreement' => 'is:agreed'
]);
length
Verifies that the value is string with the precisely this length:
$validator = Validator::create([
'param' => 'length:20'
]);
max
Verifies that value is numeric with the maximum value of:
$validator = Validator::create([
'param' => 'max:100'
]);
maxLength
Verifies that the value is string with the maximum length of:
$validator = Validator::create([
'param' => 'maxLength:20'
]);
min
Verifies that the value is numeric with the minimum value of:
$validator = Validator::create([
'param' => 'min:1'
]);
minLength
Verifies that value is string with the minimum length of:
$validator = Validator::create([
'param' => 'minLength:10'
]);
numeric
Verifies that the value is numeric by using PHP's is_numeric() function.
$validator = Validator::create([
'param' => 'numeric'
]);
present
Verifies that this parameter is present in the data set being validated, although parameter is allowed to be empty.
$validator = Validator::create([
'param' => 'present'
]);
required
Verifies that this parameter is present in data set and not empty.
$validator = Validator::create([
'param' => 'required'
]);
same
Verifies that the parameter has the same value as other parameter in the same data set.
$validator = Validator::create([
'password' => 'required',
'password_confirm' => 'same:password'
]);
slug
Verifies that value is the URL slug containing only small characters a-z, dashes (-
) and numbers 0-9:
$validator = Validator::create([
'param' => 'slug'
]);
You may check the slug helper for more info.
startsWith
Verifies that value starts with the string from parameter:
$validator = Validator::create([
'name' => 'startsWith:John',
'phone' => 'startsWith:+', // phone parameter must start with "+"
]);
You may check the slug helper for more info.
unique
This rule will check the database for the presence of value and it'll say it's correct if value is not found in database, kinda like opposite from exists rule. If value is not present in database, it means that value is unique.
Let's say you have a sign up form and you want to check if entered email already exists in database.
$validator = Validator::create([
'user_email' => 'required|email|unique:\Db\User,email'
]);
Other example: let's say user is signed into your system and wants to change its email address. You want to validate if entered email doesn't already exist in your table, but you want to ignore user's records because - user might submit the form without changing its email address and you probably don't want to say that email is incorrect.
$validator = Validator::create([
'user_email' => 'required|email|unique:\Db\User,email,5'
]);
This example will search the user
table, but ignoring record(s) where primary key is 5
. You can ignore other column
by defining it:
$validator = Validator::create([
'user_email' => 'required|email|unique:\Db\User,email,5,id'
]);
Now, it's explicitly said that rule should ignore records where id=5
.
Sometimes, value you want to ignore is present in data set you're validating, so in that case, you can reference it like:
$validator = Validator::create([
'user_id' => 'required|integer|min:1',
'user_email' => 'required|email|unique:\Db\User,email,field:user_id,id'
]);