Oke we need to create two files and we need to update one file.
Note that I use the namespace of App. If you changed the namespace or place it somewhere else you need to update the namespace of course ;)
Now let's start with step one, we will create a service provider for the this.
ValidationServiceProvider
Now that we know the name of the service provider, let's create the file and add some code. We create a new directory called validation in the services directory in the app directory. A lot of directories :P
app\Services\Validation\ValidationServiceProvider.php
<?php namespace App\Services\Validation;
use Illuminate\Support\ServiceProvider;
class ValidationServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// We don't have to register anything here so we keep this empty!
}
/**
* Boot the service provider.
*/
public function boot()
{
// Need to override the default validator with our own validator
// We can do that by using the resolver function
$this->app->validator->resolver(function ($translator, $data, $rules, $messages)
{
// This class will hold all our custom validations
return new CustomValidation($translator, $data, $rules, $messages);
});
}
}
CustomValidation
Now our next step is to create the CustomValidation class. We can do that in the same directory as the service provider
app\Services\Validation\CustomValidation.php
<?php namespace App\Services\Validation;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Validator;
// It's important to extend the Validator class
// By doing that we make sure we keep all the other rules as well!
// Now if you want to override a rule you can do that here as well
// For now we will only focus on creating rules
class CustomValidation extends Validator {
// Laravel keeps a certain convention for rules
// So the function is called validateGreaterThen
// Then the rule is greater_then
// A validation rule accepts three parameters
// $attribute This is the name of the input
// $value This is the value of the input
// $parameters This is a parameter for the rule, so greater_then:1,2 has two parameters
// the $parameters are returned as an array so for the first parameter: $parameters[0]
// Now that we know how a rule works let's create one
/**
* $attribute Input name
* $value Input value
* $parameters Table, field1
*/
public function validateUniqueWith($attribute, $value, $parameters)
{
// Now that we have our data we can check for the data
// We first grab the correct table which is passed to the function
// Now we need to do some checking using Eloquent
// If you don't understand this, please let me know
$result = DB::table($parameters[0])->where(function($query) use ($attribute, $value, $parameters) {
$query->where($attribute, '=', $value) // where firstname = value
->orWhere($parameters[1]), '=', $value); // where lastname = value
})->first();
// Now we check if we have a record
// If we do have a record we return false and the validation will fail
// If we can't find a record we return true and the validation will succeed
return $result ? false : true;
}
// If you need more examples, let me know ;)
}
Register the service provider
Now we have a service provider with a custom validation class but Laravel doesn't know where to find it. We can simply update the providers array in the app config
config\app.php
'providers' => [
// other providers
// Add the new provider at the bottom
'App\Services\Validation\ValidationServiceProvider',
],
All done
Now that we are done we can use the rule like this
$rules = [
'firstname' => 'required|unique_with:users,lastname'
];
More information on service providers: http://laravel.com/docs/master/container