2017 © Pedro Peláez
 

library laravel-validation

Laravel Validation Service

image

prettus/laravel-validation

Laravel Validation Service

  • Sunday, February 4, 2018
  • by andersonandrade
  • Repository
  • 10 Watchers
  • 237 Stars
  • 922,278 Installations
  • PHP
  • 47 Dependents
  • 10 Suggesters
  • 41 Forks
  • 18 Open issues
  • 9 Versions
  • 13 % Grown

The README.md

Laravel Validation Service

Total Downloads Latest Stable Version Latest Unstable Version License, (*1)

Installation

Add "prettus/laravel-repository": "1.1.*" to composer.json, (*2)

"prettus/laravel-validation": "1.1.*"

Create a validator

The Validator contains rules for adding, editing., (*3)

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, (*4)

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:, (*5)


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, (*6)


protected $messages = [ 'required' => 'The :attribute field is required.', ];

Or, you may wish to specify a custom error messages only for a specific field., (*7)


protected $messages = [ 'email.required' => 'We need to know your e-mail address!', ];

Custom Attributes

You too may use custom name attributes, (*8)


protected $attributes = [ 'email' => 'E-mail', 'obs' => 'Observation', ];

Using the Validator


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 - contato@andersonandra.de, (*9)

Credits

http://culttt.com/2014/01/13/advanced-validation-service-laravel-4/, (*10)

The Versions

04/02 2018

dev-develop

dev-develop http://andersao.github.io/laravel-validation

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation

04/02 2018

dev-master

9999999-dev http://andersao.github.io/laravel-validation

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation

29/08 2017

1.1.5

1.1.5.0

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation

09/06 2015

1.1.4

1.1.4.0

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation

07/04 2015

1.1.3

1.1.3.0

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation

24/03 2015

1.1.2

1.1.2.0

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation

21/03 2015

1.1.1

1.1.1.0

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation

21/03 2015

1.1.0

1.1.0.0

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation

26/01 2015

1.0.0

1.0.0.0

Laravel Validation Service

  Sources   Download

The Requires

 

laravel service validation