Validate email attribute only if it's present

Question

I want email validation rule to fail only when email attribute is present. From what I can tell, email validation is performed unconditionally in Laravel.

To give you a bigger picture, the site I'm writing allows to log in with email/password, or with social networks (socialite). And not every network reveals user email. And I'm in the process of moving validation rules to models (watson/validating package).


Show source
| laravel   | validation   | php   | email-validation   2017-01-07 14:01 1 Answers

Answers ( 1 )

  1. 2017-01-07 14:01

    As it can be found in the documentation, you can use the sometimes rule which validates the corresponding field only when it is present:

    $rules = [
        'email' => 'sometimes|required|email|otherValidationsHere',
    ];
    
◀ Go back