Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/calsranna/validator

A simple encapsulation of Illuminate\Contracts\Validation\Factory in Laravel.
https://github.com/calsranna/validator

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

# Validator

[![Build Status](https://travis-ci.org/Su9Tail/Validator.svg?branch=master)](https://travis-ci.org/Su9Tail/Validator)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Su9Tail/Validator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Su9Tail/Validator/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/Su9Tail/Validator/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Su9Tail/Validator/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/Su9Tail/Validator/badges/build.png?b=master)](https://scrutinizer-ci.com/g/Su9Tail/Validator/build-status/master)
[![Latest Stable Version](https://poser.pugx.org/cals/validator/v/stable)](https://packagist.org/packages/cals/validator)
[![Total Downloads](https://poser.pugx.org/cals/validator/downloads)](https://packagist.org/packages/cals/validator)
[![Latest Unstable Version](https://poser.pugx.org/cals/validator/v/unstable)](https://packagist.org/packages/cals/validator)
[![License](https://poser.pugx.org/cals/validator/license)](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

You can simply install Validator use composer.

```Bash
composer require cals/validator
```

And then add the `Cals\Validator\ValidatorServiceProvider::class` to your `config/app.php` providers array.

```PHP
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

You have to publish the config using this command:

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

You should put your rules and messages in it.

## 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.

```JSON
{
"errors": {
"username": [
"用户名不能为空"
],
"password": [
"密码不能是字母、数字、破折号和下划线之外的其他字符",
"密码必须在 6 到 18 位之间"
]
}
}
```

## Example

Validator suggest using like this.

```PHP
validator = $validator;
}

public function index(Request $request)
{
$values = $request->all();
$this->validator->validate($values,'user');
}
}

```

## License

The Validator is open-sourced library licensed under the [MIT license](http://opensource.org/licenses/MIT).