2017 © Pedro Peláez
 

library validation

A laravel 4 validation package

image

michaeljennings/validation

A laravel 4 validation package

  • Monday, April 27, 2015
  • by michaeljennings
  • Repository
  • 1 Watchers
  • 0 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Validation Latest Stable Version Latest Unstable Version License

A laravel 4 validation package aiming to help you clean up your controllers and models, and make validation quicker and simpler., (*1)

Installation

Include the package in your composer.json., (*2)

  
"michaeljennings/validation": "1.0"
  

Run composer install or composer update to download the dependencies., (*3)

Once the package has been downloaded add the validation service provider to the list of service providers in app/config/app.php., (*4)

  
'providers' => array(

  'Michaeljennings\Validation\ValidationServiceProvider'
  
);
  

Add the Validation facade to your aliases array., (*5)

  
'aliases' => array(

  'Validation' => 'Michaeljennings\Validation\Facades\Validation',
  
);
  

Publish the config files using the php artisan config:publish michaeljennings/validation, (*6)

By default the validators are store in a app/validators.php so you may need to create this file. Alternatively if you want to store your validators else where you can update the path in the package config., (*7)

Usage

Creating a Validator

To create a new validator we use the add function. This function takes two arguments, a name for the validator to be called by and a closure with the rules for the validator., (*8)

  
Validation::add('exampleValidator', function($validator)
{

  $validator->rule('name')->required();
  
});
  

To break this down the Validation::add('exampleValidator', function($validator) {}); adds a new validator into a collection with the name 'exampleValidator'., (*9)

We then add all of our validation rules on the $validator object. To create a new rule we use the rule function and then we can chain the laravel validation rules on the rule object., (*10)

For example if we wanted to validate an email field to make sure a value was passed and that the value was a valid email address we could use $validator->rule('email')->required()->email();., (*11)

The validation rules can either be camel cased or snake cased so both $validator->('foo')->requiredWith('bar'); and $validator->('foo')->required_with('bar'); are valid., (*12)

<, (*13)

p>To see all of the available validation rules see the laravel docs., (*14)

Validation Error Messages

If you need to set a different validation error message we can use the message function. The message function takes two arguments, the validation rule the message is shown for and the error message., (*15)

  
$validator->rule('foo')->required()->message('required', 'This is a different validation message');
  

Using a Validator

To run a validator we use the make function. This function takes two arguments, the name of the validator we want to run and the input to be validated. When we call the make function the validator is bound to the validation class so we can call any of the laravel validation functions on the Validation facade., (*16)

  
Validation::make('exampleValidator', Input::all());

if (Validation::passes()) {
 // Handle success
} else {
  return Redirect::back()->withErrors(Validation::errors());
}
  

You can also chain functions when using the make function, for example:, (*17)

  
if (Validation::make('exampleValidator', Input::all())->fails()) {
  return Redirect::back()->withErrors(Validation::errors());
}
  

If you need to add a rule to the validator after it has been made you can do so by using the rule function to create the new rules and then the createRules function to update the validators rules., (*18)

  
Validation::make('exampleValidator', Input::all());

Validation::rule('foo')->required();
Validation::createRules();
  

The Versions

27/04 2015

dev-master

9999999-dev

A laravel 4 validation package

  Sources   Download

MIT

The Requires

 

by Michael Jennings

27/12 2014

1.0.x-dev

1.0.9999999.9999999-dev

A laravel 4 validation package

  Sources   Download

The Requires

 

by Michael Jennings

27/12 2014

v1.0

1.0.0.0

A laravel 4 validation package

  Sources   Download

The Requires

 

by Michael Jennings