Laravel provides a variety of validation rules that improve productivity. We can easily manage and customize the validation as per the requirement. You can also use the Rule
class while defining the validation rules. Let’s use the exits validation rule.
'email' => 'exists:connection.staff,email'
Now if we want to customize the query executed by the validation rule, then we have to use the Rule class.
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::exists('staff')->where(function ($query) {
$query->where('account_id', 1);
}),
],
]);
But there are some cases when we need the custom validation.
Using Custom Validation
Laravel provides a variety of validation rules, however, you can define your own custom validation rules. One is registering a custom validation rule is using a rule object. You may use the make:rule
artisan command to generate a new rule object.
php artisan make:rule Uppercase
Above command will create an Uppercase class at app/Rules directory. The Uppercase class structure should be similar to the below codes.
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Uppercase implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
//
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The validation error message.';
}
}
Now you can define the custom rule logic in passes()
method and the related error message in message()
method.
...
public function passes($attribute, $value)
{
return strtoupper($value) === $value;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must be uppercase.';
}
..
Use The Custom Validation Rule
Using the defined custom validation rule is pretty easy.
use App\Rules\Uppercase;
$request->validate([
'name' => ['required', 'string', new Uppercase],
]);
Custom Validation Rule Using Closures
If you need the custom validation rule only once then you don’t need to generate the validation rule using Rule object. You may use the Closure. The closure receives the 3 attributes i.e. the name, the value and a fails callback.
$validator = Validator::make($request->all(), [
'title' => [
'required',
'string',
function ($attribute, $value, $fail) {
if ($value === 'strtoupper($value)) {
$fail($attribute.' is invalid.');
}
},
],
]);
Validation Rule using Extend method
You can also define the custom validation rule by using the extend method on the Validator facade. We can use this method within a service provider to register a custom validation rule. So let’s open the AppServiceProvider and define the custom validation rule.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('uppercase', function ($attribute, $value, $parameters, $validator) {
return $value === strtoupper($value);
});
}
}