Package Data | |
---|---|
Maintainer Username: | Cals |
Maintainer Contact: | iloveyou62948@163.com (Cals) |
Package Create Date: | 2016-12-13 |
Package Last Update: | 2018-07-04 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:06:59 |
Package Statistics | |
---|---|
Total Downloads: | 53 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Validator is designed for Laravel when use ajax. Validator is a simple encapsulation of Illuminate\Contracts\Validation\Factory in Laravel, which excepted validating data easier.
You can simply install Validator use composer.
composer require cals/validator
And then add the Cals\Validator\ValidatorServiceProvider::class
to your config/app.php
providers array.
Cals\Validator\ValidatorServiceProvider::class
If you use Laravel 5.5 and after, you don't need do it cause laravel will auto load the provider.
You have to publish the config using this command:
php artisan vendor:publish --tag="validator"
You should put your rules and messages in it.
Validator provides a simple way to validate data, you can simply use it anywhere you want.
validate(array $values = [], $resource, array $messages = [], $sometimes = false)
$values
is the data you wish to validate, $resource
is one of your key in rules
which contained in validator.php
. And you can set messages while validating fails to return by using $message
.When $sometimes
was true
, rules in sometimes would be used.
When validate failed, Validator will send a json response automatically.The returned data is like this.
{
"errors": {
"username": [
"用户名不能为空"
],
"password": [
"密码不能是字母、数字、破折号和下划线之外的其他字符",
"密码必须在 6 到 18 位之间"
]
}
}
Validator suggest using like this.
<?php
namespace App\Http\Controllers;
use Cals\Validator\AjaxValidator;
use Illuminate\Http\Request;
class ExampleController extends Controller
{
private $validator;
public function __construct(AjaxValidator $validator)
{
$this->validator = $validator;
}
public function index(Request $request)
{
$values = $request->all();
$this->validator->validate($values,'user');
}
}
The Validator is open-sourced library licensed under the MIT license.