Download the PHP package rakit/validation without Composer
On this page you can find all versions of the php package rakit/validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rakit/validation
More information about rakit/validation
Files in rakit/validation
Package validation
Short Description PHP Laravel like standalone validation library
License MIT
Informations about the package validation
Rakit Validation - PHP Standalone Validation Library
PHP Standalone library for validating data. Inspired by Illuminate\Validation
Laravel.
Features
- API like Laravel validation.
- Array validation.
$_FILES
validation with multiple file support.- Custom attribute aliases.
- Custom validation messages.
- Custom rule.
Requirements
- PHP 7.0 or higher
- Composer for installation
Quick Start
Installation
Usage
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
:
or just validate
it:
In this case, 2 examples above will output the same results.
But with make
you can setup something like custom invalid message, custom attribute alias, etc before validation running.
Attribute Alias
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:
Now if province_id
value is empty, error message would be 'Province is required'.
Custom Validation Message
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.
And also there are several message variables depends on their rules.
Here are some ways to register/set your custom message(s):
Custom Messages for Validator
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:
Or using setMessages
method like this:
Custom Messages for Validation
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:
Or you can use $validation->setMessages
like this:
Custom Message for Specific Attribute Rule
Sometimes you may want to set custom message for specific rule attribute.
To do this you can use :
as message separator or using chaining methods.
Examples:
Or using chaining methods:
Translation
Translation is different with custom messages.
Translation may needed when you use custom message for rule in
, not_in
, mimes
, and uploaded_file
.
For example if you use rule in:1,2,3
we will set invalid message like "The Attribute only allows '1', '2', or '3'"
where part "'1', '2', or '3'" is comes from ":allowed_values" tag.
So if you have custom Indonesian message ":attribute hanya memperbolehkan :allowed_values",
we will set invalid message like "Attribute hanya memperbolehkan '1', '2', or '3'" which is the "or" word is not part of Indonesian language.
So, to solve this problem, we can use translation like this:
Actually, our built-in rules only use words 'and' and 'or' that you may need to translates.
Working with Error Message
Errors messages are collected in Rakit\Validation\ErrorBag
object that you can get it using errors()
method.
Now you can use methods below to retrieves errors messages:
all(string $format = ':message')
Get all messages as flatten array.
Examples:
firstOfAll(string $format = ':message', bool $dotNotation = false)
Get only first message from all existing keys.
Examples:
Argument $dotNotation
is for array validation.
If it is false
it will return original array structure, if it true
it will return flatten array with dot notation keys.
For example:
first(string $key)
Get first message from given key. It will return string
if key has any error message, or null
if key has no errors.
For example:
toArray()
Get all messages grouped by it's keys.
For example:
count()
Get count messages.
has(string $key)
Check if given key has an error. It returns bool
if a key has an error, and otherwise.
Getting Validated, Valid, and Invalid Data
For example you have validation like this:
You can get validated data, valid data, or invalid data using methods in example below:
Available Rules
Click to show details.
required
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`.required_if:another_field,value_1,value_2,...
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'`.required_unless:another_field,value_1,value_2,...
The field under validation must be present and not empty unless the anotherfield field is equal to any value.required_with:field_1,field_2,...
The field under validation must be present and not empty only if any of the other specified fields are present.required_without:field_1,field_2,...
The field under validation must be present and not empty only when any of the other specified fields are not present.required_with_all:field_1,field_2,...
The field under validation must be present and not empty only if all of the other specified fields are present.required_without_all:field_1,field_2,...
The field under validation must be present and not empty only when all of the other specified fields are not present.uploaded_file:min_size,max_size,extension_a,extension_b,...
This rule will validate data from `$_FILES`. Field under this rule must be follows rules below to be valid: * `$_FILES['key']['error']` must be `UPLOAD_ERR_OK` or `UPLOAD_ERR_NO_FILE`. For `UPLOAD_ERR_NO_FILE` you can validate it with `required` 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: * `uploaded_file`: uploaded file is optional. When it is not empty, it must be `ERR_UPLOAD_OK`. * `required|uploaded_file`: uploaded file is required, and it must be `ERR_UPLOAD_OK`. * `uploaded_file:0,1M`: uploaded file size must be between 0 - 1 MB, but uploaded file is optional. * `required|uploaded_file:0,1M,png,jpeg`: uploaded file size must be between 0 - 1MB and mime types must be `image/jpeg` or `image/png`. Optionally, if you want to have separate error message between size and type validation. You can use `mimes` rule to validate file types, and `min`, `max`, or `between` to validate it's size. For multiple file upload, PHP will give you undesirable array `$_FILES` structure ([here](http://php.net/manual/en/features.file-upload.multiple.php#53240) is the topic). So we make `uploaded_file` rule to automatically resolve your `$_FILES` value to be well-organized array structure. That means, you cannot only use `min`, `max`, `between`, or `mimes` rules to validate multiple file upload. You should put `uploaded_file` just to resolve it's value and make sure that value is correct uploaded file value. For example if you have input files like this: You can simply validate it like this: Or if you have input files like this: You can validate it like this: Now when you use `getValidData()` or `getInvalidData()` you will get well array structure just like single file upload.mimes:extension_a,extension_b,...
The `$_FILES` item under validation must have a MIME type corresponding to one of the listed extensions.default/defaults
This is special rule that doesn't validate anything. It just set default value to your attribute if that attribute is empty or not present. For example if you have validation like this Validation passes because we sets default value for `enabled` and `published` to `1` and `0` which is valid. Then we can get the valid/default data.uppercase
The field under this validation must be valid uppercase.lowercase
The field under this validation must be valid lowercase.json
The field under this validation must be valid JSON string.alpha
The field under this rule must be entirely alphabetic characters.numeric
The field under this rule must be numeric.alpha_num
The field under this rule must be entirely alpha-numeric characters.alpha_dash
The field under this rule may have alpha-numeric characters, as well as dashes and underscores.alpha_spaces
The field under this rule may have alpha characters, as well as spaces.in:value_1,value_2,...
The field under this rule must be included in the given list of values. This rule is using `in_array` to check the value. By default `in_array` disable strict checking. So it doesn't check data type. If you want enable strict checking, you can invoke validator like this: Then 'enabled' value should be boolean `true`, or int `1`.not_in:value_1,value_2,...
The field under this rule must not be included in the given list of values. This rule also using `in_array`. You can enable strict checking by invoking validator and call `strict()` like example in rule `in` above.min:number
The field under this rule must have a size greater or equal than the given number. For string value, size corresponds to the number of characters. For integer or float value, size corresponds to its numerical value. For an array, size corresponds to the count of the array. If your value is numeric string, you can put `numeric` rule to treat its size by numeric value instead of number of characters. You can also validate uploaded file using this rule to validate minimum size of uploaded file. For example:max:number
The field under this rule must have a size lower or equal than the given number. Value size calculated in same way like `min` rule. You can also validate uploaded file using this rule to validate maximum size of uploaded file. For example:between:min,max
The field under this rule must have a size between min and max params. Value size calculated in same way like `min` and `max` rule. You can also validate uploaded file using this rule to validate size of uploaded file. For example:digits:value
The field under validation must be numeric and must have an exact length of `value`.digits_between:min,max
The field under validation must have a length between the given `min` and `max`.url
The field under this rule must be valid url format. By default it check common URL scheme format like `any_scheme://...`. But you can specify URL schemes if you want. For example: > For common URL scheme and mailto, we combine `FILTER_VALIDATE_URL` to validate URL format and `preg_match` to validate it's scheme. Except for JDBC URL, currently it just check a valid JDBC scheme.integer
The field under t rule must be integer.boolean
The field under this rule must be boolean. Accepted input are `true`, `false`, `1`, `0`, `"1"`, and `"0"`.ip
The field under this rule must be valid ipv4 or ipv6.ipv4
The field under this rule must be valid ipv4.ipv6
The field under this rule must be valid ipv6.extension:extension_a,extension_b,...
The field under this rule must end with an extension corresponding to one of those listed. This is useful for validating a file type for a given a path or url. The `mimes` rule should be used for validating uploads.array
The field under this rule must be array.same:another_field
The field value under this rule must be same with `another_field` value.regex:/your-regex/
The field under this rule must be match with given regex.date:format
The field under this rule must be valid date format. Parameter `format` is optional, default format is `Y-m-d`.accepted
The field under this rule must be one of `'on'`, `'yes'`, `'1'`, `'true'`, or `true`.present
The field under this rule must be exists, whatever the value is.different:another_field
Opposite of `same`. The field value under this rule must be different with `another_field` value.after:tomorrow
Anything that can be parsed by `strtotime` can be passed as a parameter to this rule. Valid examples include : - after:next week - after:2016-12-31 - after:2016 - after:2016-12-31 09:56:02before:yesterday
This also works the same way as the [after rule](#after). Pass anything that can be parsed by `strtotime`callback
You can use this rule to define your own validation rule. This rule can't be registered using string pipe. To use this rule, you should put Closure inside array of rules. For example: You can set invalid message by returning a string. For example, example above would be: > Note: `Rakit\Validation\Rules\Callback` instance is binded into your Closure. So you can access rule properties and methods using `$this`.nullable
Field under this rule may be empty.Register/Override Rule
Another way to use custom validation rule is to create a class extending Rakit\Validation\Rule
.
Then register it using setValidator
or addValidator
.
For example, you want to create unique
validator that check field availability from database.
First, lets create UniqueRule
class:
Then you need to register UniqueRule
instance into validator like this:
Now you can use it like this:
In UniqueRule
above, property $message
is used for default invalid message. And property $fillable_params
is used for fillParameters
method (defined in Rakit\Validation\Rule
class). By default fillParameters
will fill parameters listed in $fillable_params
. For example unique:users,email,[email protected]
in example above, will set:
If you want your custom rule accept parameter list like
in
,not_in
, oruploaded_file
rules, you just need to overridefillParameters(array $params)
method in your custom rule class.
Note that unique
rule that we created above also can be used like this:
So you can improve UniqueRule
class above by adding some methods that returning its own instance like this:
Then you can use it in more funky way like this:
Implicit Rule
Implicit rule is a rule that if it's invalid, then next rules will be ignored. For example if attribute didn't pass required*
rules, mostly it's next rules will also be invalids. So to prevent our next rules messages to get collected, we make required*
rules to be implicit.
To make your custom rule implicit, you can make $implicit
property value to be true
. For example:
Modify Value
In some case, you may want your custom rule to be able to modify it's attribute value like our default/defaults
rule. So in current and next rules checks, your modified value will be used.
To do this, you should implements Rakit\Validation\Rules\Interfaces\ModifyValue
and create method modifyValue($value)
to your custom rule class.
For example:
Before Validation Hook
You may want to do some preparation before validation running. For example our uploaded_file
rule will resolves attribute value that come from $_FILES
(undesirable) array structure to be well-organized array structure, so we can validate multiple file upload just like validating other data.
To do this, you should implements Rakit\Validation\Rules\Interfaces\BeforeValidate
and create method beforeValidate()
to your custom rule class.
For example:
All versions of validation with dependencies
ext-mbstring Version *