Rakit Validation - PHP Standalone Validation Library
======================================================
[![Build Status](https://img.shields.io/travis/rakit/validation.svg?style=flat-square)](https://travis-ci.org/rakit/validation)
[![Coverage Status](https://coveralls.io/repos/github/rakit/validation/badge.svg?branch=setup_coveralls)](https://coveralls.io/github/rakit/validation)
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://doge.mit-license.org)
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
```
composer require "rakit/validation"
```
#### 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`:
```php
<?php
require('vendor/autoload.php');
use Rakit\Validation\Validator;
$validator = new Validator;
// make it
$validation = $validator->make($_POST + $_FILES, [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|min:6',
'confirm_password' => 'required|same:password',
'avatar' => 'required|uploaded_file:0,500K,png,jpeg',
'skills' => 'array',
'skills.*.id' => 'required|numeric',
'skills.*.percentage' => 'required|numeric'
]);
// 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
<?php
require('vendor/autoload.php');
use Rakit\Validation\Validator;
$validator = new Validator;
$validation = $validator->validate($_POST + $_FILES, [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|min:6',
'confirm_password' => 'required|same:password',
'avatar' => 'required|uploaded_file:0,500K,png,jpeg',
'skills' => 'array',
'skills.*.id' => 'required|numeric',
'skills.*.percentage' => 'required|numeric'
]);
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 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:
```php
$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'.
## 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:
```php
$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:
```php
$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);
```
#### 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:
```php
$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:
```php
$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();
```
#### 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:
```php
$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:
```php
$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();
```
## 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:
```php
// Set translation for words 'and' and 'or'.
$validator->setTranslations([
'and' => 'dan',
'or' => 'atau'
]);
// Set custom message for 'in' rule
$validator->setMessage('in', ":attribute hanya memperbolehkan :allowed_values");
// Validate
$validation = $validator->validate($inputs, [
'nomor' => 'in:1,2,3'
]);
$message = $validation->errors()->first('nomor'); // "Nomor hanya memperbolehkan '1', '2', atau '3'"
```
> 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\Er
没有合适的资源?快使用搜索试试~ 我知道了~
【WordPress插件】2022年最新版完整功能demo+插件v9.3.8.zip
共4150个文件
php:3906个
png:96个
json:31个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 147 浏览量
2022-04-03
07:17:11
上传
评论
收藏 5.41MB ZIP 举报
温馨提示
"【WordPress插件】2022年最新版完整功能demo+插件v9.3.8 Smart Notification Wordpress Plugin. Web & Mobile Push, FB Messenger, FB Notifications & Newsletter 智能通知WordPress插件。 Web和移动推送,FB Messenger,FB通知和通讯" ---------- 泰森云每天更新发布最新WordPress主题、HTML主题、WordPress插件、shopify主题、opencart主题、PHP项目源码、安卓项目源码、ios项目源码,更有超10000个资源可供选择,如有需要请站内联系。
资源推荐
资源详情
资源评论
收起资源包目录
【WordPress插件】2022年最新版完整功能demo+插件v9.3.8.zip (4150个子文件)
random_compat.phar.pubkey.asc 488B
AUTHORS 279B
openssl.cnf 104B
COPYING 11KB
fb_ca_chain_bundle.crt 7KB
smoothness.css 33KB
emojionearea.css 29KB
selectize.css 21KB
select2.min.css 17KB
frontend.css 13KB
smio-style.css 10KB
tooltipster.bundle.min.css 7KB
smio-progressbar.css 4KB
jquery-labelauty.css 3KB
jquery-ui-timepicker-addon.min.css 2KB
autoload-style38.css 251B
autoload-style.css 205B
smio-style-rtl.css 190B
.php_cs.dist 2KB
phpstan.neon.dist 840B
phpunit.xml.dist 749B
.editorconfig 135B
wpspin_light-2x.gif 9KB
wpspin_light.gif 2KB
animated-overlay.gif 2KB
unnamed.gif 43B
.gitattributes 125B
.gitignore 22B
.gitignore 14B
amp-permission-dialog.html 10KB
amp-helper-iframe.html 7KB
Chart.bundle.min.js 197KB
Chart.min.js 149KB
emojionearea.js 91KB
select2.min.js 62KB
moment.min.js 50KB
selectize.min.js 44KB
jquery-ui-timepicker-addon.min.js 40KB
tooltipster.bundle.min.js 39KB
smio-function.js 26KB
smio-plugins.js 16KB
jquery.progressbar.js 12KB
amp_sw.js 5KB
gmap.js 4KB
jquery-ui-sliderAccess.js 3KB
jquery-labelauty.js 2KB
frontend.js 1KB
firebase_sw.js 1KB
sw.js 1KB
BF-ecommerce-template.json 95KB
BF-newsletter-template.json 89KB
BF-basic-e-commerce.json 71KB
BF-basic-newsletter.json 62KB
BF-promo-template.json 55KB
BF-simple-template.json 36KB
BF-basic-standard.json 33KB
installed.json 32KB
BF-basic-onecolumn.json 31KB
composer.json 3KB
composer.json 2KB
composer.json 2KB
BF-blank-template.json 2KB
composer.json 1KB
installed.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 924B
composer.json 870B
composer.json 796B
composer.json 680B
composer.json 635B
composer.json 621B
composer.json 561B
composer.json 513B
composer.json 465B
composer.json 451B
composer.json 211B
composer.json 165B
composer.json 149B
jwt_header 194B
LICENSE 10KB
LICENSE 10KB
LICENSE 10KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
Makefile 189B
CHANGELOG.md 82KB
UPGRADING.md 52KB
共 4150 条
- 1
- 2
- 3
- 4
- 5
- 6
- 42
资源评论
Lee达森
- 粉丝: 957
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功