vector-kerr / laravel-validation by Vector88

Validation Utilities for Laravel
23
1
3
Package Data
Maintainer Username: Vector88
Maintainer Contact: vector.kerr@gmail.com (Daniel Kerr)
Package Create Date: 2016-08-17
Package Last Update: 2016-08-21
Language: PHP
License: MIT
Last Refreshed: 2024-11-20 03:03:26
Package Statistics
Total Downloads: 23
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 1
Total Watchers: 3
Total Forks: 0
Total Open Issues: 0

Laravel Validation

This package provides validation utilities for Laravel. The utilities are intended to be generic and expandable, allowing the same set of validation rules to be reused between different systems.

Installation

Add the package using the composer command line interface:

composer require vector88/laravel-validation

Add the service provider to config/app.php:

[
	...

	'providers' => [
		...,
		Vector88\Laravel\Validation\ValidationServiceProvider::class,
		...,
	],

	'aliases' => [
		...,
		'Validation' => Vector88\Laravel\Validation\Facades\Validation::class,
		...,
	],

	...

]

Example

App/Http/routes.php

Route::get('/laravel', 'ValidationController@laravel' );
Route::get('/angular', 'ValidationController@angular' );
Route::get('/validator', 'ValidationController@validator' );

App/Http/Controllers/ValidationController.php

<?php

namespace App\Http\Controllers;

use Validator;
use Validation;

class ValidationController extends Controller
{
	
	protected $validation;
	
	public function __construct() {
		$this->validation =
			Validation::make()
				->field( 'name' )->isRequired()->isString()->hasMin( 4 )->hasMax( 16 )
				->field( 'age' )->isInteger()->hasMin( 0 )->hasMax( 120 );
	}
	
	public function angular() {
		return $this->validation
			->provide( 'angular' )
			->allAttributes();
	}
	
	public function laravel() {
		return $this->validation
			->provide( 'laravel' )
			->allAttributes();
	}
	
	public function validator() {
		$data = [ 'name' => 'Jon', 'age' => 148.7 ];
		$rules = $this->validation->rules();
        $validator = Validator::make( $data, $rules );
		return $validator->errors();
	}
}