markeilander / validator by markeilander

Laravel 5 validator package.
144
0
1
Package Data
Maintainer Username: markeilander
Maintainer Contact: mark.eilander@outlook.com (Mark Eilander)
Package Create Date: 2016-02-16
Package Last Update: 2018-09-17
Language: PHP
License: MIT
Last Refreshed: 2024-11-19 03:03:56
Package Statistics
Total Downloads: 144
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

Laravel 5 Validator

Table of contents

[TOC]

Usage

This package is based on laravel validation: http://laravel.com/docs/5.1/validation

Create a validator


use Eilander\Validator\LaravelValidator as Validator;

class UpdateUserValidator extends Validator
{
    protected static $rules = [
        'name'     => 'required',
        'email'    => 'required|email',
        'msg'      => 'required'
    ];
}

Custom Error Messages

You may use custom error messages for validation instead of the defaults http://laravel.com/docs/5.1/validation#working-with-error-messages


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

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


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

Using the Validator


use \Eilander\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()
    {
        if ($this->validator->fails($request->all())) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }

        // do something
    }
}