PHP Standalone library for validating data. Inspired by Illuminate\Validation
Laravel.
- PHP 5.5 or higher
- Composer for installation
composer require "rakit/validation"
There are two ways to validating data with this library. Using make
to make validation object,
then validate it using validate
. Or just use validate
.
Examples:
Using make
:
<?php
require('vendor/autoload.php');
use Rakit\Validation\Validator;
$validator = new Validator;
// make it
$validation = $validator->make($_POST, [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|min:6',
'confirm_password' => 'required|same:password',
]);
// then validate
$validation->validate();
if ($validation->fails()) {
// handling errors
$errors = $validation->errors();
echo "<pre>";
print_r($errors->firstOfAll());
echo "</pre>";
exit;
} else {
// validation passes
echo "Success!";
}
or just validate
it:
<?php
require('vendor/autoload.php');
use Rakit\Validation\Validator;
$validator = new Validator;
$validation = $validator->validate($_POST, [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|min:6',
'confirm_password' => 'required|same:password',
]);
if ($validation->fails()) {
// handling errors
$errors = $validation->errors();
echo "<pre>";
print_r($errors->firstOfAll());
echo "</pre>";
exit;
} else {
// validation passes
echo "Success!";
}
In this case, 2 example above will output the same results.
But with make
you can setup something like custom invalid message, custom attribute alias, etc before validation running.
By default we will transform your attribute into more readable text. For example confirm_password
will be displayed as Confirm password
.
But you can set it anything you want with setAlias
or setAliases
method.
Example:
$validator = new Validator;
// To set attribute alias, you should use `make` instead `validate`.
$validation->make([
'province_id' => $_POST['province_id'],
'district_id' => $_POST['district_id']
], [
'province_id' => 'required|numeric',
'district_id' => 'required|numeric'
]);
// now you can set aliases using this way:
$validation->setAlias('province_id', 'Province');
$validation->setAlias('district_id', 'District');
// or this way:
$validation->setAliases([
'province_id' => 'Province',
'district_id' => 'District'
]);
// then validate it
$validation->validate();
Now if province_id
value is empty, error message would be 'Province is required'.
Before register/set custom messages, here are some variables you can use in your custom messages:
:attribute
: will replaced into attribute alias.:value
: will replaced into stringify value of attribute. For array and object will replaced to json.:params[n]
: will replaced into rule parameter,n
is index array. For example:params[0]
inmin:6
will replaced into6
.
And here are some ways to register/set your custom message(s):
With this way, anytime you make validation using make
or validate
it will set your custom messages for it.
It is useful for localization.
To do this, you can set custom messages as first argument constructor like this:
$validator = new Validator([
'required' => ':attribute harus diisi',
'email' => ':email tidak valid',
// etc
]);
// then validation belows will use those custom messages
$validation_a = $validator->validate($dataset_a, $rules_for_a);
$validation_b = $validator->validate($dataset_b, $rules_for_b);
Or using setMessages
method like this:
$validator = new Validator;
$validator->setMessages([
'required' => ':attribute harus diisi',
'email' => ':email tidak valid',
// etc
]);
// now validation belows will use those custom messages
$validation_a = $validator->validate($dataset_a, $rules_for_dataset_a);
$validation_b = $validator->validate($dataset_b, $rules_for_dataset_b);
Sometimes you may want to set custom messages for specific validation.
To do this you can set your custom messages as 3rd argument of $validator->make
or $validator->validate
like this:
$validator = new Validator;
$validation_a = $validator->validate($dataset_a, $rules_for_dataset_a, [
'required' => ':attribute harus diisi',
'email' => ':email tidak valid',
// etc
]);
Or you can use $validation->setMessages
like this:
$validator = new Validator;
$validation_a = $validator->make($dataset_a, $rules_for_dataset_a);
$validation_a->setMessages([
'required' => ':attribute harus diisi',
'email' => ':email tidak valid',
// etc
]);
...
$validation_a->validate();
Sometimes you may want to set custom message for specific rule attribute.
To do this you can use .
as message separator or using chaining method.
Examples:
$validator = new Validator;
$validation_a = $validator->make($dataset_a, [
'age' => 'required|min:18'
]);
$validation_a->setMessages([
'age.min' => '18+ only',
]);
$validation_a->validate();
Or using chaining methods:
$validator = new Validator;
$validation_a = $validator->make($dataset_a, [
'photo' => [
'required',
$validator('uploaded_file')->fileTypes('jpeg|png')->message('Photo must be jpeg/png image')
]
]);
$validation_a->validate();
Below is list of all available validation rules
- required
- required_if
- uploaded_file
- alpha
- numeric
- alpha_num
- alpha_dash
- in
- not_in
- min
- max
- between
- url
- ip
- ipv4
- ipv6
- array
- same
- regex
- date
- accepted
- present
- different
The field under this validation must be present and not 'empty'.
Here are some examples:
Value | Valid |
---|---|
'something' |
true |
'0' |
true |
0 |
true |
[0] |
true |
[null] |
true |
null | false |
[] | false |
'' | false |
For uploaded file, $_FILES['key']['error']
must not UPLOAD_ERR_NO_FILE
.
The field under this rule must be present and not empty if the anotherfield field is equal to any value.
For example required_if:something,1,yes,on
will be required if something
value is one of 1
, '1'
, 'yes'
, or 'on'
.
This rule will validate $_FILES
data, but not for multiple uploaded files.
Field under this rule must be following rules below to be valid:
$_FILES['key']['error']
must beUPLOAD_ERR_OK
orUPLOAD_ERR_NO_FILE
. ForUPLOAD_ERR_NO_FILE
you can validate it withrequired
rule.- If min size is given, uploaded file size MUST NOT be lower than min size.
- If max size is given, uploaded file size MUST NOT be higher than max size.
- If file types is given, mime type must be one of those given types.
Here are some example definitions and explanations:
Definition | Explanation |
---|---|
uploaded_file |
Uploaded file is optional. When it is not empty, it must be ERR_UPLOAD_OK . |
`required | uploaded_file` |
uploaded_file:0,1M |
uploaded file size must be between 0 - 1 MB, but uploaded file are optional |
`required | uploaded_file:0,1M,png,jpeg` |
The field under this validation must be valid email address.
The field under this rule must be entirely alphabetic characters.
The field under this rule must be numeric.
The field under this rule must be entirely alpha-numeric characters.
The field under this rule may have alpha-numeric characters, as well as dashes and underscores.
The field under this rule must be included in the given list of values.
The field under this rule must not be included in the given list of values.
soon ...
soon ...
soon ...
The field under this rule must be valid url format.
The field under this rule must be valid ipv4 or ipv6.
The field under this rule must be valid ipv4.
The field under this rule must be valid ipv6.
The field under this rule must be array.
The field value under this rule must be same with another_field
value.
The field under this rule must be match with given regex.
The field under this rule must be valid date format. Parameter format
is optional, default format is Y-m-d
.
The field under this rule must be one of 'on'
, 'yes'
, '1'
, 'true'
, or true
.
The field under this rule must be exists, whatever the value is.
Opposite of same
. The field value under this rule must be different with another_field
value.
Soon ...