Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andersao/laravel-validator
Laravel Validation Service
https://github.com/andersao/laravel-validator
Last synced: 3 days ago
JSON representation
Laravel Validation Service
- Host: GitHub
- URL: https://github.com/andersao/laravel-validator
- Owner: andersao
- License: mit
- Created: 2015-01-25T04:55:13.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-05-05T23:35:01.000Z (8 months ago)
- Last Synced: 2024-12-31T18:44:36.878Z (6 days ago)
- Language: PHP
- Homepage: http://andersao.github.io/laravel-validator
- Size: 40 KB
- Stars: 412
- Watchers: 10
- Forks: 82
- Open Issues: 25
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Validation Service
[![Total Downloads](https://poser.pugx.org/prettus/laravel-validation/downloads.svg)](https://packagist.org/packages/prettus/laravel-validation)
[![Latest Stable Version](https://poser.pugx.org/prettus/laravel-validation/v/stable.svg)](https://packagist.org/packages/prettus/laravel-validation)
[![Latest Unstable Version](https://poser.pugx.org/prettus/laravel-validation/v/unstable.svg)](https://packagist.org/packages/prettus/laravel-validation)
[![License](https://poser.pugx.org/prettus/laravel-validation/license.svg)](https://packagist.org/packages/prettus/laravel-validation)## Installation
Add "prettus/laravel-repository": "1.1.*" to composer.json
```json
"prettus/laravel-validation": "1.1.*"
```
### Create a validatorThe Validator contains rules for adding, editing.
```php
Prettus\Validator\Contracts\ValidatorInterface::RULE_CREATE
Prettus\Validator\Contracts\ValidatorInterface::RULE_UPDATE
```
In the example below, we define some rules for both creation and edition```php
use \Prettus\Validator\LaravelValidator;class PostValidator extends LaravelValidator {
protected $rules = [
'title' => 'required',
'text' => 'min:3',
'author'=> 'required'
];}
```
To define specific rules, proceed as shown below:
```php
use \Prettus\Validator\LaravelValidator;
class PostValidator extends LaravelValidator {
protected $rules = [
ValidatorInterface::RULE_CREATE => [
'title' => 'required',
'text' => 'min:3',
'author'=> 'required'
],
ValidatorInterface::RULE_UPDATE => [
'title' => 'required'
]
];}
```
### Custom Error Messages
You may use custom error messages for validation instead of the defaults
```php
protected $messages = [
'required' => 'The :attribute field is required.',
];```
Or, you may wish to specify a custom error messages only for a specific field.
```php
protected $messages = [
'email.required' => 'We need to know your e-mail address!',
];```
### Custom Attributes
You too may use custom name attributes
```php
protected $attributes = [
'email' => 'E-mail',
'obs' => 'Observation',
];```
### Using the Validator
```php
use \Prettus\Validator\Exceptions\ValidatorException;
class PostsController extends BaseController {
/**
* @var PostRepository
*/
protected $repository;
/**
* @var PostValidator
*/
protected $validator;public function __construct(PostRepository $repository, PostValidator $validator){
$this->repository = $repository;
$this->validator = $validator;
}
public function store()
{try {
$this->validator->with( Input::all() )->passesOrFail();
// OR $this->validator->with( Input::all() )->passesOrFail( ValidatorInterface::RULE_CREATE );$post = $this->repository->create( Input::all() );
return Response::json([
'message'=>'Post created',
'data' =>$post->toArray()
]);} catch (ValidatorException $e) {
return Response::json([
'error' =>true,
'message' =>$e->getMessage()
]);}
}public function update($id)
{try{
$this->validator->with( Input::all() )->passesOrFail( ValidatorInterface::RULE_UPDATE );
$post = $this->repository->update( Input::all(), $id );return Response::json([
'message'=>'Post created',
'data' =>$post->toArray()
]);}catch (ValidatorException $e){
return Response::json([
'error' =>true,
'message' =>$e->getMessage()
]);}
}
}
```# Author
Anderson Andrade -
## Credits
http://culttt.com/2014/01/13/advanced-validation-service-laravel-4/