Laravel 5.7 - 5.8 custom backend validation rules in 3.5 minutes

So, this is my first post to this website. Today, I’ll try to share with you some useful thing for Laravel developer.

![Laravel validation rules](/storage/uploads/photos/1/Screen Shot 2019-05-13 at 2.40.35 PM.png)

![Laravel validation rules](/storage/uploads/photos/1/Screen Shot 2019-05-13 at 2.40.35 PM.png)

So, this is my first post to this website. Today, I’ll try to share with you some useful thing for Laravel developer.

I assume that you already know how to install Laravel and have implemented simple html form. So now you’ll know how to:

  1. Make custom rule class
  2. Set validation error message
  3. Create localization for validation error message
  4. Use custom validation rule in controller with class name
  5. Use custom validation rule in controller with rule shortcut

Before start create form with at least two fields:

  • Full name
  • Phone number

Something like this:

At first we will create validation rule for full name field.

1. Make custom validation rule class

So let’s make new rule with artisan command:

php artisan make:rule AlphaSpace

You’ll get new file in your ~laravel-project/app/rules folder.

Now you just need to add your logic to method passes. So, for your purpose we’ll add this regular expression.

return preg_match('/^[\pL\s]+$/u', $value);

namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;

class AlphaSpace implements Rule
{
    public function __construct()
    {
        //
    }

    public function passes($attribute, $value)
    {
        //
    }

    public function message()
    {
        return 'The validation error message.';
    }
}

3. Create localization for validation error message

To set validation error message, you guess, we’ll use method message. So just replace default error message with:

return preg_match('/^[\pL\s]+$/u', $value);

And go to the localization file and find this lines:

'custom' => [
    'attribute-name' => [
        'rule-name' => 'custom-message',
    ],
],

Next lets place above those lines our new error message localization like this:

'alpha_space' => 'The :attribute must contain letters and spaces only!',

4. Use custom validation rule in controller with class name

Time to check if it works. Wrap your existing rule in array and add our new validation rule this way.

$validatedData = $request->validate([
    	'name' => ['required','max:255', new AlphaSpace],
    	'email' => 'required|email',
    	'phone' => 'required|phone_number',
	]);

If validation fails and all went fine you should see validation error message we defined in previous part.

5. Use custom validation rule in controller with rule shortcut

So, what if you want to use native Laravel syntax. Then, there is pretty good solution.

You can extend Validator class with your new rule shortcut in boot method of App\ServiceProvider.php. So you’ll end up with something like this.

public function boot()
{
	Validator::extend('alpha_space', 'App\\Rules\\AlphaSpace@passes');
}

After this manipulation you can use rules with shortcuts in controllers, custom requests or anywhere else. So, change validation in your controller to this.

$validatedData = $request->validate([
    'name' => 'required|max:255|alpha_space',
    'email' => 'required|email',
    'phone' => 'required|phone_number',
]);

If you prefer more visual tutorials check the video version.

If you fine any errors or you know how to do it in more convenient way, please add your opinion in the comment section of my youtube channel.

Thanks for your time.