Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simply validate email in laravel without creating validator

Tags:

laravel

How can I simply validate an email in laravel, without creating a validator?

Validator::email($email)

does not work

like image 266
Alex Avatar asked Oct 30 '25 01:10

Alex


1 Answers

By laravel ref

We can do like this Validator::make(['email' => $email], [ 'email' => 'email'])

OR

By using laravel helper we can do like this

 validator(['email' => $email], ['email' => 'required|email']);

OR

Edit: We can further simplefy this by using macro

Add this in Service provider

$this
            ->app
            ->make(Validator::class)
            ->macro('email', function ($email) {
                return Validator::make(['email' => $email], [ 'email' => 'email'])
            });

And now we can use like this

Validator::email($email)

Note: Not test this just throwing some idea we can do this way.

like image 152
vijaykumar Avatar answered Oct 31 '25 20:10

vijaykumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!