Search code examples
phplaravelvalidationemail-validation

Validate email attribute only if it's present


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).


Solution

  • 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',
    ];