michaeljennings/validation

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

A laravel 4 validation package

v1.0 2014-12-27 18:29 UTC

This package is auto-updated.

Last update: 2024-11-06 08:43:46 UTC


README

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

Installation

Include the package in your composer.json.

  
"michaeljennings/validation": "1.0"
  

Run composer install or composer update to download the dependencies.

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

  
'providers' => array(

'Michaeljennings\Validation\ValidationServiceProvider'

);

Add the Validation facade to your aliases array.

  
'aliases' => array(

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

);

Publish the config files using the php artisan config:publish michaeljennings/validation

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.

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.

  
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'.

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.

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();.

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.

To see all of the available validation rules see the laravel docs.

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.

  
$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.

  
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:

  
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.

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

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