PHPackages                             cals/validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. cals/validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

cals/validator
==============

A simple encapsulation of Illuminate\\Contracts\\Validation\\Factory in Laravel.

v1.0.1(8y ago)054MITPHPPHP &gt;=5.5.9

Since Dec 13Pushed 8y ago1 watchersCompare

[ Source](https://github.com/Su9Tail/Validator)[ Packagist](https://packagist.org/packages/cals/validator)[ RSS](/packages/cals-validator/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (13)Used By (0)

Validator
=========

[](#validator)

[![Build Status](https://camo.githubusercontent.com/28a9e3495812e4a2bd046c82cf8f0f46736d49b3dc92bef7eb092bea41bbd10f/68747470733a2f2f7472617669732d63692e6f72672f5375395461696c2f56616c696461746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Su9Tail/Validator)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/081c914fe1efc23ea0c372eac9d3a3ff8e7415397879025de1470641d573147e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5375395461696c2f56616c696461746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Su9Tail/Validator/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/42168f028713fe54caa8bd962bc14d659383491e27948f43a6c184728cabadba/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5375395461696c2f56616c696461746f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Su9Tail/Validator/?branch=master)[![Build Status](https://camo.githubusercontent.com/cc0cdb2c32fc6184d6f2ba899c1f3ec86f0d774f5cbd746358c6c6262fa32ecf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5375395461696c2f56616c696461746f722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Su9Tail/Validator/build-status/master)[![Latest Stable Version](https://camo.githubusercontent.com/74a2230ac880c9303182aaf7c5b19767aade2cc2b509570318d5202b88463643/68747470733a2f2f706f7365722e707567782e6f72672f63616c732f76616c696461746f722f762f737461626c65)](https://packagist.org/packages/cals/validator)[![Total Downloads](https://camo.githubusercontent.com/6a25937aa4845dbfa5a8e976e0211456ad175ada5c5df41bf1a47f6537d97235/68747470733a2f2f706f7365722e707567782e6f72672f63616c732f76616c696461746f722f646f776e6c6f616473)](https://packagist.org/packages/cals/validator)[![Latest Unstable Version](https://camo.githubusercontent.com/4a2c87d557fc7e818cbc1e12480af6eb9f97d6d897102837658fa9bc6d9f38a8/68747470733a2f2f706f7365722e707567782e6f72672f63616c732f76616c696461746f722f762f756e737461626c65)](https://packagist.org/packages/cals/validator)[![License](https://camo.githubusercontent.com/3fb12f37e316d4a70cc40a3591bee267b0c6b63155728dd4202970eb2c6d9d43/68747470733a2f2f706f7365722e707567782e6f72672f63616c732f76616c696461746f722f6c6963656e7365)](https://packagist.org/packages/cals/validator)

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.

Install
-------

[](#install)

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.

Configuration
-------------

[](#configuration)

You have to publish the config using this command:

```
php artisan vendor:publish --tag="validator"
```

You should put your rules and messages in it.

Usage
-----

[](#usage)

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 位之间"
        ]
    }
}
```

Example
-------

[](#example)

Validator suggest using like this.

```
