laravellaravel-7laravel-validation

Replace Laravel's default email validation policy


By default, Laravel's email validation does not fail on invalid email, eg 'example@example'.

$data = ['email' => 'example@example'];
$rules = ['email' => 'email'];
$v = Validator::make($data, $rules); // success

This would be ok, but it should be default:

$data = ['email' => 'example@example'];
$rules = ['email' => 'email:filter'];
$v = Validator::make($data, $rules); // fails

How can I set default e-mail validation policy?

Laravel version is 7.


Solution

  • I don't think you can override the existing rules, but instead you can try adding new ones in app/providers/AppServiceProvider.php

    class AppServiceProvider extends ServiceProvider
    {
        public function boot(): void
        {
            \Validator::extend('email_default', function ($attribute, $value) {
                return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
            });
        }
    }