ivva
ivva

Reputation: 2949

How to validate email field using required_if and email rules in Laravel

I'm validating a form and if a checkbox is_external_user has value of 1, I want to make the email field required and validate the type email.

Here's my validation rule:

 'email' => [
    'validation'    => 
      'required_if:Users.is_external_user,1|email',
 ],

required_if is working as expected, but type email validation returns the error that it is not a valid email address without being required.

Is there working way to use both require_if and email rules?

Upvotes: 4

Views: 1864

Answers (1)

ivva
ivva

Reputation: 2949

I found a decision:

'email' => [
    'validation'   => 'sometimes|nullable|required_if:Users.is_external_user,1|email',
]

I've added sometimes|nullable to the rules.

Upvotes: 7

Related Questions