izytech/laravel-validation

There is no license information available for the latest version (v1.0.1) of this package.

Laravel Validation Service

v1.0.1 2018-06-30 14:35 UTC

This package is not auto-updated.

Last update: 2024-04-22 18:15:21 UTC


README

Total Downloads Latest Stable Version Latest Unstable Version License

Installation

Add "izytech/laravel-repository": "1.0.*" to composer.json

"izytech/laravel-validation": "1.0.*"

Create a validator

The Validator contains rules for adding, editing.

IzyTech\Validator\Contracts\ValidatorInterface::RULE_CREATE
IzyTech\Validator\Contracts\ValidatorInterface::RULE_UPDATE

In the example below, we define some rules for both creation and edition

use \IzyTech\Validator\LaravelValidator;

class PostValidator extends LaravelValidator {

    protected $rules = [
        'title' => 'required',
        'text'  => 'min:3',
        'author'=> 'required'
    ];

}

To define specific rules, proceed as shown below:


use \IzyTech\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


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

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


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

Custom Attributes

You too may use custom name attributes


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

Using the Validator


use \IzyTech\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

Ulrich Ntella - ulrichsoft2002@gmail.com